author | wenzelm |
Thu, 14 Aug 2008 16:52:54 +0200 | |
changeset 27868 | a28b3cd0077b |
parent 27845 | 141772c866c9 |
child 28054 | 2b84d34c5d02 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Tools/code/code_target.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Serializer from intermediate language ("Thin-gol") |
|
6 |
to target languages (like SML or Haskell). |
|
7 |
*) |
|
8 |
||
9 |
signature CODE_TARGET = |
|
10 |
sig |
|
11 |
include BASIC_CODE_THINGOL; |
|
12 |
||
13 |
val add_syntax_class: string -> class |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
14 |
-> (string * (string * string) list) option -> theory -> theory; |
24219 | 15 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory; |
16 |
val add_syntax_tycoP: string -> string -> OuterParse.token list |
|
17 |
-> (theory -> theory) * OuterParse.token list; |
|
18 |
val add_syntax_constP: string -> string -> OuterParse.token list |
|
19 |
-> (theory -> theory) * OuterParse.token list; |
|
20 |
||
21 |
val add_undefined: string -> string -> string -> theory -> theory; |
|
22 |
val add_pretty_list: string -> string -> string -> theory -> theory; |
|
23 |
val add_pretty_list_string: string -> string -> string |
|
24 |
-> string -> string list -> theory -> theory; |
|
25 |
val add_pretty_char: string -> string -> string list -> theory -> theory |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
26 |
val add_pretty_numeral: string -> bool -> bool -> string -> string -> string |
24219 | 27 |
-> string -> string -> theory -> theory; |
24992 | 28 |
val add_pretty_message: string -> string -> string list -> string |
24219 | 29 |
-> string -> string -> theory -> theory; |
30 |
||
27103 | 31 |
val allow_abort: string -> theory -> theory; |
24841 | 32 |
|
27000 | 33 |
type serialization; |
24219 | 34 |
type serializer; |
27103 | 35 |
val add_target: string * serializer -> theory -> theory; |
27550 | 36 |
val extend_target: string * (string * (CodeThingol.program -> CodeThingol.program)) |
37 |
-> theory -> theory; |
|
27103 | 38 |
val assert_target: theory -> string -> string; |
39 |
val serialize: theory -> string -> string option -> Args.T list |
|
40 |
-> CodeThingol.program -> string list -> serialization; |
|
27000 | 41 |
val compile: serialization -> unit; |
27304 | 42 |
val export: serialization -> unit; |
27000 | 43 |
val file: Path.T -> serialization -> unit; |
27304 | 44 |
val string: string list -> serialization -> string; |
27103 | 45 |
|
27304 | 46 |
val code_of: theory -> string -> string -> string list -> string list -> string; |
27103 | 47 |
val eval_conv: string * (unit -> thm) option ref |
48 |
-> theory -> cterm -> string list -> thm; |
|
49 |
val eval_term: string * (unit -> 'a) option ref |
|
50 |
-> theory -> term -> string list -> 'a; |
|
51 |
val shell_command: string (*theory name*) -> string (*cg expr*) -> unit; |
|
24219 | 52 |
|
53 |
val setup: theory -> theory; |
|
27103 | 54 |
val code_width: int ref; |
27437 | 55 |
|
56 |
val ml_code_of: theory -> CodeThingol.program -> string list -> string * string list; |
|
24219 | 57 |
end; |
58 |
||
59 |
structure CodeTarget : CODE_TARGET = |
|
60 |
struct |
|
61 |
||
62 |
open BasicCodeThingol; |
|
63 |
||
64 |
(** basics **) |
|
65 |
||
66 |
infixr 5 @@; |
|
67 |
infixr 5 @|; |
|
68 |
fun x @@ y = [x, y]; |
|
69 |
fun xs @| y = xs @ [y]; |
|
24634 | 70 |
val str = PrintMode.setmp [] Pretty.str; |
24219 | 71 |
val concat = Pretty.block o Pretty.breaks; |
72 |
val brackets = Pretty.enclose "(" ")" o Pretty.breaks; |
|
73 |
fun semicolon ps = Pretty.block [concat ps, str ";"]; |
|
25771 | 74 |
fun enum_default default sep opn cls [] = str default |
75 |
| enum_default default sep opn cls xs = Pretty.enum sep opn cls xs; |
|
24219 | 76 |
|
27304 | 77 |
datatype destination = Compile | Export | File of Path.T | String of string list; |
27436 | 78 |
type serialization = destination -> (string * string list) option; |
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
79 |
|
27103 | 80 |
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*) |
81 |
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f); |
|
82 |
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n"; |
|
83 |
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p; |
|
26998 | 84 |
|
27103 | 85 |
(*FIXME why another code_setmp?*) |
86 |
fun compile f = (code_setmp f Compile; ()); |
|
27304 | 87 |
fun export f = (code_setmp f Export; ()); |
27103 | 88 |
fun file p f = (code_setmp f (File p); ()); |
27436 | 89 |
fun string cs f = fst (the (code_setmp f (String cs))); |
27304 | 90 |
|
91 |
fun stmt_names_of_destination (String stmts) = stmts |
|
92 |
| stmt_names_of_destination _ = []; |
|
27014
a5f53d9d2b60
yet another attempt to circumvent printmode problems
haftmann
parents:
27001
diff
changeset
|
93 |
|
24219 | 94 |
|
27000 | 95 |
(** generic syntax **) |
24219 | 96 |
|
97 |
datatype lrx = L | R | X; |
|
98 |
||
99 |
datatype fixity = |
|
100 |
BR |
|
101 |
| NOBR |
|
102 |
| INFX of (int * lrx); |
|
103 |
||
104 |
val APP = INFX (~1, L); |
|
105 |
||
26010 | 106 |
fun fixity_lrx L L = false |
107 |
| fixity_lrx R R = false |
|
108 |
| fixity_lrx _ _ = true; |
|
24219 | 109 |
|
27024 | 110 |
fun fixity NOBR _ = false |
111 |
| fixity _ NOBR = false |
|
26010 | 112 |
| fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) = |
24219 | 113 |
pr < pr_ctxt |
114 |
orelse pr = pr_ctxt |
|
26010 | 115 |
andalso fixity_lrx lr lr_ctxt |
24219 | 116 |
orelse pr_ctxt = ~1 |
27024 | 117 |
| fixity BR (INFX _) = false |
26010 | 118 |
| fixity _ _ = true; |
24219 | 119 |
|
120 |
fun gen_brackify _ [p] = p |
|
121 |
| gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps |
|
122 |
| gen_brackify false (ps as _::_) = Pretty.block ps; |
|
123 |
||
27024 | 124 |
fun brackify fxy_ctxt = |
125 |
gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks; |
|
24219 | 126 |
|
27024 | 127 |
fun brackify_infix infx fxy_ctxt = |
128 |
gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks; |
|
24219 | 129 |
|
130 |
type class_syntax = string * (string -> string option); |
|
131 |
type typ_syntax = int * ((fixity -> itype -> Pretty.T) |
|
132 |
-> fixity -> itype list -> Pretty.T); |
|
133 |
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T) |
|
27304 | 134 |
-> thm -> bool -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T); |
24219 | 135 |
|
27550 | 136 |
datatype name_syntax_table = NameSyntaxTable of { |
137 |
class: class_syntax Symtab.table, |
|
138 |
inst: unit Symtab.table, |
|
139 |
tyco: typ_syntax Symtab.table, |
|
140 |
const: term_syntax Symtab.table |
|
141 |
}; |
|
142 |
||
24219 | 143 |
|
27000 | 144 |
(** theory data **) |
145 |
||
146 |
val target_SML = "SML"; |
|
147 |
val target_OCaml = "OCaml"; |
|
148 |
val target_Haskell = "Haskell"; |
|
149 |
||
27024 | 150 |
fun mk_name_syntax_table ((class, inst), (tyco, const)) = |
151 |
NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const }; |
|
152 |
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) = |
|
153 |
mk_name_syntax_table (f ((class, inst), (tyco, const))); |
|
154 |
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 }, |
|
155 |
NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = |
|
156 |
mk_name_syntax_table ( |
|
27000 | 157 |
(Symtab.join (K snd) (class1, class2), |
158 |
Symtab.join (K snd) (inst1, inst2)), |
|
159 |
(Symtab.join (K snd) (tyco1, tyco2), |
|
160 |
Symtab.join (K snd) (const1, const2)) |
|
161 |
); |
|
162 |
||
27304 | 163 |
type serializer = |
27024 | 164 |
string option (*module name*) |
165 |
-> Args.T list (*arguments*) |
|
166 |
-> (string -> string) (*labelled_name*) |
|
167 |
-> string list (*reserved symbols*) |
|
168 |
-> (string * Pretty.T) list (*includes*) |
|
169 |
-> (string -> string option) (*module aliasses*) |
|
27000 | 170 |
-> (string -> class_syntax option) |
171 |
-> (string -> typ_syntax option) |
|
172 |
-> (string -> term_syntax option) |
|
27103 | 173 |
-> CodeThingol.program |
27024 | 174 |
-> string list (*selected statements*) |
175 |
-> serialization; |
|
24219 | 176 |
|
27550 | 177 |
datatype serializer_entry = Serializer of serializer |
178 |
| Extends of string * (CodeThingol.program -> CodeThingol.program); |
|
179 |
||
27000 | 180 |
datatype target = Target of { |
181 |
serial: serial, |
|
27550 | 182 |
serializer: serializer_entry, |
27000 | 183 |
reserved: string list, |
184 |
includes: Pretty.T Symtab.table, |
|
27024 | 185 |
name_syntax_table: name_syntax_table, |
27000 | 186 |
module_alias: string Symtab.table |
187 |
}; |
|
24219 | 188 |
|
27024 | 189 |
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) = |
27000 | 190 |
Target { serial = serial, serializer = serializer, reserved = reserved, |
27024 | 191 |
includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias }; |
192 |
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) = |
|
193 |
mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias)))); |
|
27550 | 194 |
fun merge_target strict target (Target { serial = serial1, serializer = serializer, |
27000 | 195 |
reserved = reserved1, includes = includes1, |
27024 | 196 |
name_syntax_table = name_syntax_table1, module_alias = module_alias1 }, |
27000 | 197 |
Target { serial = serial2, serializer = _, |
198 |
reserved = reserved2, includes = includes2, |
|
27024 | 199 |
name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) = |
27550 | 200 |
if serial1 = serial2 orelse not strict then |
27000 | 201 |
mk_target ((serial1, serializer), |
202 |
((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)), |
|
27024 | 203 |
(merge_name_syntax_table (name_syntax_table1, name_syntax_table2), |
27000 | 204 |
Symtab.join (K snd) (module_alias1, module_alias2)) |
205 |
)) |
|
206 |
else |
|
207 |
error ("Incompatible serializers: " ^ quote target); |
|
208 |
||
209 |
structure CodeTargetData = TheoryDataFun |
|
210 |
( |
|
211 |
type T = target Symtab.table * string list; |
|
212 |
val empty = (Symtab.empty, []); |
|
213 |
val copy = I; |
|
214 |
val extend = I; |
|
215 |
fun merge _ ((target1, exc1) : T, (target2, exc2)) = |
|
27550 | 216 |
(Symtab.join (merge_target true) (target1, target2), Library.merge (op =) (exc1, exc2)); |
27000 | 217 |
); |
218 |
||
219 |
fun the_serializer (Target { serializer, ... }) = serializer; |
|
220 |
fun the_reserved (Target { reserved, ... }) = reserved; |
|
221 |
fun the_includes (Target { includes, ... }) = includes; |
|
27024 | 222 |
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x; |
27000 | 223 |
fun the_module_alias (Target { module_alias , ... }) = module_alias; |
224 |
||
27103 | 225 |
val abort_allowed = snd o CodeTargetData.get; |
226 |
||
227 |
fun assert_target thy target = |
|
27000 | 228 |
case Symtab.lookup (fst (CodeTargetData.get thy)) target |
229 |
of SOME data => target |
|
230 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
231 |
||
27550 | 232 |
fun put_target (target, seri) thy = |
24219 | 233 |
let |
27550 | 234 |
val defined_target = is_some o Symtab.lookup (fst (CodeTargetData.get thy)); |
235 |
val _ = case seri |
|
236 |
of Extends (super, _) => if defined_target super then () |
|
27710 | 237 |
else error ("Unknown code target language: " ^ quote super) |
27550 | 238 |
| _ => (); |
239 |
val _ = if defined_target target |
|
240 |
then warning ("Overwriting existing target " ^ quote target) |
|
241 |
else (); |
|
24219 | 242 |
in |
27000 | 243 |
thy |
244 |
|> (CodeTargetData.map o apfst oo Symtab.map_default) |
|
245 |
(target, mk_target ((serial (), seri), (([], Symtab.empty), |
|
27024 | 246 |
(mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), |
27000 | 247 |
Symtab.empty)))) |
248 |
((map_target o apfst o apsnd o K) seri) |
|
24219 | 249 |
end; |
250 |
||
27550 | 251 |
fun add_target (target, seri) = put_target (target, Serializer seri); |
252 |
fun extend_target (target, (super, modify)) = |
|
253 |
put_target (target, Extends (super, modify)); |
|
254 |
||
27103 | 255 |
fun map_target_data target f thy = |
24219 | 256 |
let |
27103 | 257 |
val _ = assert_target thy target; |
24219 | 258 |
in |
27000 | 259 |
thy |
260 |
|> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f |
|
24219 | 261 |
end; |
262 |
||
27103 | 263 |
fun map_reserved target = |
264 |
map_target_data target o apsnd o apfst o apfst; |
|
265 |
fun map_includes target = |
|
266 |
map_target_data target o apsnd o apfst o apsnd; |
|
27024 | 267 |
fun map_name_syntax target = |
27103 | 268 |
map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table; |
27000 | 269 |
fun map_module_alias target = |
27103 | 270 |
map_target_data target o apsnd o apsnd o apsnd; |
27000 | 271 |
|
27550 | 272 |
fun invoke_serializer thy modify abortable serializer reserved includes |
27103 | 273 |
module_alias class inst tyco const module args program1 cs1 = |
24219 | 274 |
let |
27550 | 275 |
val program2 = modify program1; |
27103 | 276 |
val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const; |
277 |
val cs2 = subtract (op =) hidden cs1; |
|
27550 | 278 |
val program3 = Graph.subgraph (not o member (op =) hidden) program2; |
27103 | 279 |
val all_cs = Graph.all_succs program2 cs2; |
27550 | 280 |
val program4 = Graph.subgraph (member (op =) all_cs) program3; |
27103 | 281 |
val empty_funs = filter_out (member (op =) abortable) |
282 |
(CodeThingol.empty_funs program3); |
|
283 |
val _ = if null empty_funs then () else error ("No defining equations for " |
|
284 |
^ commas (map (CodeName.labelled_name thy) empty_funs)); |
|
285 |
in |
|
286 |
serializer module args (CodeName.labelled_name thy) reserved includes |
|
287 |
(Symtab.lookup module_alias) (Symtab.lookup class) |
|
288 |
(Symtab.lookup tyco) (Symtab.lookup const) |
|
27550 | 289 |
program4 cs2 |
27103 | 290 |
end; |
291 |
||
292 |
fun mount_serializer thy alt_serializer target = |
|
293 |
let |
|
294 |
val (targets, abortable) = CodeTargetData.get thy; |
|
27550 | 295 |
fun collapse_hierarchy target = |
296 |
let |
|
297 |
val data = case Symtab.lookup targets target |
|
298 |
of SOME data => data |
|
299 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
300 |
in case the_serializer data |
|
301 |
of Serializer _ => (I, data) |
|
302 |
| Extends (super, modify) => let |
|
303 |
val (modify', data') = collapse_hierarchy super |
|
304 |
in (modify' #> modify, merge_target false target (data', data)) end |
|
305 |
end; |
|
306 |
val (modify, data) = collapse_hierarchy target; |
|
307 |
val serializer = the_default (case the_serializer data |
|
308 |
of Serializer seri => seri) alt_serializer; |
|
27000 | 309 |
val reserved = the_reserved data; |
310 |
val includes = Symtab.dest (the_includes data); |
|
27103 | 311 |
val module_alias = the_module_alias data; |
27024 | 312 |
val { class, inst, tyco, const } = the_name_syntax data; |
27103 | 313 |
in |
27550 | 314 |
invoke_serializer thy modify abortable serializer reserved |
27103 | 315 |
includes module_alias class inst tyco const |
24219 | 316 |
end; |
317 |
||
27103 | 318 |
fun serialize thy = mount_serializer thy NONE; |
27000 | 319 |
|
24219 | 320 |
fun parse_args f args = |
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27757
diff
changeset
|
321 |
case Scan.read OuterLex.stopper f args |
24219 | 322 |
of SOME x => x |
323 |
| NONE => error "Bad serializer arguments"; |
|
324 |
||
325 |
||
27103 | 326 |
(** generic code combinators **) |
24219 | 327 |
|
328 |
(* list, char, string, numeral and monad abstract syntax transformations *) |
|
329 |
||
27304 | 330 |
fun nerror thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm thm); |
331 |
||
24219 | 332 |
fun implode_list c_nil c_cons t = |
333 |
let |
|
334 |
fun dest_cons (IConst (c, _) `$ t1 `$ t2) = |
|
335 |
if c = c_cons |
|
336 |
then SOME (t1, t2) |
|
337 |
else NONE |
|
338 |
| dest_cons _ = NONE; |
|
339 |
val (ts, t') = CodeThingol.unfoldr dest_cons t; |
|
340 |
in case t' |
|
341 |
of IConst (c, _) => if c = c_nil then SOME ts else NONE |
|
342 |
| _ => NONE |
|
343 |
end; |
|
344 |
||
345 |
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) = |
|
346 |
let |
|
347 |
fun idx c = find_index (curry (op =) c) c_nibbles; |
|
348 |
fun decode ~1 _ = NONE |
|
349 |
| decode _ ~1 = NONE |
|
350 |
| decode n m = SOME (chr (n * 16 + m)); |
|
351 |
in decode (idx c1) (idx c2) end |
|
352 |
| decode_char _ _ = NONE; |
|
353 |
||
354 |
fun implode_string c_char c_nibbles mk_char mk_string ts = |
|
355 |
let |
|
356 |
fun implode_char (IConst (c, _) `$ t1 `$ t2) = |
|
357 |
if c = c_char then decode_char c_nibbles (t1, t2) else NONE |
|
358 |
| implode_char _ = NONE; |
|
359 |
val ts' = map implode_char ts; |
|
360 |
in if forall is_some ts' |
|
361 |
then (SOME o str o mk_string o implode o map_filter I) ts' |
|
362 |
else NONE |
|
363 |
end; |
|
364 |
||
27304 | 365 |
fun implode_numeral thm negative c_pls c_min c_bit0 c_bit1 = |
24219 | 366 |
let |
25936 | 367 |
fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0 |
368 |
else if c = c_bit1 then 1 |
|
27304 | 369 |
else nerror thm "Illegal numeral expression: illegal bit" |
370 |
| dest_bit _ = nerror thm "Illegal numeral expression: illegal bit"; |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
371 |
fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0 |
25936 | 372 |
else if c = c_min then |
373 |
if negative then SOME ~1 else NONE |
|
27304 | 374 |
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
|
375 |
| 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
|
376 |
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
|
377 |
in case n of SOME n => SOME (2 * n + b) | NONE => NONE end |
27304 | 378 |
| dest_numeral _ = nerror thm "Illegal numeral expression: illegal term"; |
25936 | 379 |
in dest_numeral #> the_default 0 end; |
24219 | 380 |
|
24992 | 381 |
fun implode_monad c_bind t = |
24219 | 382 |
let |
383 |
fun dest_monad (IConst (c, _) `$ t1 `$ t2) = |
|
24992 | 384 |
if c = c_bind |
24219 | 385 |
then case CodeThingol.split_abs t2 |
24992 | 386 |
of SOME (((v, pat), ty), t') => |
387 |
SOME ((SOME (((SOME v, pat), ty), true), t1), t') |
|
24219 | 388 |
| NONE => NONE |
389 |
else NONE |
|
390 |
| dest_monad t = case CodeThingol.split_let t |
|
24992 | 391 |
of SOME (((pat, ty), tbind), t') => |
392 |
SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t') |
|
24219 | 393 |
| NONE => NONE; |
394 |
in CodeThingol.unfoldr dest_monad t end; |
|
395 |
||
396 |
||
27103 | 397 |
(* applications and bindings *) |
398 |
||
27304 | 399 |
fun gen_pr_app pr_app pr_term syntax_const is_cons thm pat |
400 |
vars fxy (app as ((c, (_, tys)), ts)) = |
|
27103 | 401 |
case syntax_const c |
27304 | 402 |
of NONE => if pat andalso not (is_cons c) then |
27710 | 403 |
nerror thm "Non-constructor in pattern" |
27304 | 404 |
else brackify fxy (pr_app thm pat vars app) |
27103 | 405 |
| SOME (i, pr) => |
406 |
let |
|
407 |
val k = if i < 0 then length tys else i; |
|
27304 | 408 |
fun pr' fxy ts = pr (pr_term thm pat) thm pat vars fxy (ts ~~ curry Library.take k tys); |
27103 | 409 |
in if k = length ts |
410 |
then pr' fxy ts |
|
411 |
else if k < length ts |
|
412 |
then case chop k ts of (ts1, ts2) => |
|
27304 | 413 |
brackify fxy (pr' APP ts1 :: map (pr_term thm pat vars BR) ts2) |
27710 | 414 |
else pr_term thm pat vars fxy (CodeThingol.eta_expand k app) |
27103 | 415 |
end; |
416 |
||
27304 | 417 |
fun gen_pr_bind pr_bind pr_term thm (fxy : fixity) ((v, pat), ty : itype) vars = |
27103 | 418 |
let |
419 |
val vs = case pat |
|
420 |
of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat [] |
|
421 |
| NONE => []; |
|
422 |
val vars' = CodeName.intro_vars (the_list v) vars; |
|
423 |
val vars'' = CodeName.intro_vars vs vars'; |
|
424 |
val v' = Option.map (CodeName.lookup_var vars') v; |
|
27304 | 425 |
val pat' = Option.map (pr_term thm true vars'' fxy) pat; |
27103 | 426 |
in (pr_bind ((v', pat'), ty), vars'') end; |
427 |
||
428 |
||
27000 | 429 |
(* name auxiliary *) |
24219 | 430 |
|
431 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
|
432 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
|
433 |
||
434 |
val dest_name = |
|
435 |
apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode; |
|
436 |
||
27103 | 437 |
fun mk_name_module reserved_names module_prefix module_alias program = |
24219 | 438 |
let |
27103 | 439 |
fun mk_alias name = case module_alias name |
440 |
of SOME name' => name' |
|
441 |
| NONE => name |
|
442 |
|> NameSpace.explode |
|
443 |
|> map (fn name => (the_single o fst) (Name.variants [name] reserved_names)) |
|
444 |
|> NameSpace.implode; |
|
445 |
fun mk_prefix name = case module_prefix |
|
446 |
of SOME module_prefix => NameSpace.append module_prefix name |
|
447 |
| NONE => name; |
|
24219 | 448 |
val tab = |
449 |
Symtab.empty |
|
450 |
|> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name)) |
|
451 |
o fst o dest_name o fst) |
|
27103 | 452 |
program |
453 |
in the o Symtab.lookup tab end; |
|
24219 | 454 |
|
455 |
||
456 |
||
457 |
(** SML/OCaml serializer **) |
|
458 |
||
27103 | 459 |
datatype ml_stmt = |
27304 | 460 |
MLFuns of (string * (typscheme * ((iterm list * iterm) * thm) list)) list |
24219 | 461 |
| MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list |
24811 | 462 |
| MLClass of string * (vname * ((class * string) list * (string * itype) list)) |
24219 | 463 |
| MLClassinst of string * ((class * (string * (vname * sort) list)) |
464 |
* ((class * (string * (string * dict list list))) list |
|
27304 | 465 |
* ((string * const) * thm) list)); |
466 |
||
467 |
fun stmt_names_of (MLFuns fs) = map fst fs |
|
468 |
| stmt_names_of (MLDatas ds) = map fst ds |
|
469 |
| stmt_names_of (MLClass (c, _)) = [c] |
|
470 |
| stmt_names_of (MLClassinst (i, _)) = [i]; |
|
24219 | 471 |
|
27103 | 472 |
fun pr_sml_stmt syntax_tyco syntax_const labelled_name reserved_names deresolve is_cons = |
24219 | 473 |
let |
24992 | 474 |
val pr_label_classrel = translate_string (fn "." => "__" | c => c) |
475 |
o NameSpace.qualifier; |
|
24841 | 476 |
val pr_label_classparam = NameSpace.base o NameSpace.qualifier; |
24219 | 477 |
fun pr_dicts fxy ds = |
478 |
let |
|
479 |
fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_" |
|
480 |
| pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_"; |
|
481 |
fun pr_proj [] p = |
|
482 |
p |
|
483 |
| pr_proj [p'] p = |
|
484 |
brackets [p', p] |
|
485 |
| pr_proj (ps as _ :: _) p = |
|
486 |
brackets [Pretty.enum " o" "(" ")" ps, p]; |
|
27103 | 487 |
fun pr_dict fxy (DictConst (inst, dss)) = |
488 |
brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss) |
|
489 |
| pr_dict fxy (DictVar (classrels, v)) = |
|
490 |
pr_proj (map (str o deresolve) classrels) ((str o pr_dictvar) v) |
|
24219 | 491 |
in case ds |
492 |
of [] => str "()" |
|
27103 | 493 |
| [d] => pr_dict fxy d |
494 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds |
|
24219 | 495 |
end; |
27103 | 496 |
fun pr_tyvar_dicts vs = |
24219 | 497 |
vs |
24992 | 498 |
|> map (fn (v, sort) => map_index (fn (i, _) => |
499 |
DictVar ([], (v, (i, length sort)))) sort) |
|
24219 | 500 |
|> map (pr_dicts BR); |
501 |
fun pr_tycoexpr fxy (tyco, tys) = |
|
502 |
let |
|
27103 | 503 |
val tyco' = (str o deresolve) tyco |
24219 | 504 |
in case map (pr_typ BR) tys |
505 |
of [] => tyco' |
|
506 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
507 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
508 |
end |
|
27304 | 509 |
and pr_typ fxy (tyco `%% tys) = (case syntax_tyco tyco |
510 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
|
511 |
| SOME (i, pr) => pr pr_typ fxy tys) |
|
512 |
| pr_typ fxy (ITyVar v) = str ("'" ^ v); |
|
513 |
fun pr_term thm pat vars fxy (IConst c) = |
|
514 |
pr_app thm pat vars fxy (c, []) |
|
515 |
| pr_term thm pat vars fxy (IVar v) = |
|
24219 | 516 |
str (CodeName.lookup_var vars v) |
27304 | 517 |
| pr_term thm pat vars fxy (t as t1 `$ t2) = |
24219 | 518 |
(case CodeThingol.unfold_const_app t |
27304 | 519 |
of SOME c_ts => pr_app thm pat vars fxy c_ts |
24219 | 520 |
| NONE => |
27304 | 521 |
brackify fxy [pr_term thm pat vars NOBR t1, pr_term thm pat vars BR t2]) |
522 |
| pr_term thm pat vars fxy (t as _ `|-> _) = |
|
24219 | 523 |
let |
524 |
val (binds, t') = CodeThingol.unfold_abs t; |
|
525 |
fun pr ((v, pat), ty) = |
|
27304 | 526 |
pr_bind thm NOBR ((SOME v, pat), ty) |
24219 | 527 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
528 |
val (ps, vars') = fold_map pr binds vars; |
|
27304 | 529 |
in brackets (ps @ [pr_term thm pat vars' NOBR t']) end |
530 |
| pr_term thm pat vars fxy (ICase (cases as (_, t0))) = |
|
24992 | 531 |
(case CodeThingol.unfold_const_app t0 |
27103 | 532 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
27304 | 533 |
then pr_case thm vars fxy cases |
534 |
else pr_app thm pat vars fxy c_ts |
|
535 |
| NONE => pr_case thm vars fxy cases) |
|
536 |
and pr_app' thm pat vars (app as ((c, (iss, tys)), ts)) = |
|
24219 | 537 |
if is_cons c then let |
538 |
val k = length tys |
|
539 |
in if k < 2 then |
|
27304 | 540 |
(str o deresolve) c :: map (pr_term thm pat vars BR) ts |
24219 | 541 |
else if k = length ts then |
27304 | 542 |
[(str o deresolve) c, Pretty.enum "," "(" ")" (map (pr_term thm pat vars NOBR) ts)] |
27710 | 543 |
else [pr_term thm pat vars BR (CodeThingol.eta_expand k app)] end else |
27103 | 544 |
(str o deresolve) c |
27304 | 545 |
:: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term thm pat vars BR) ts |
546 |
and pr_app thm pat vars = gen_pr_app pr_app' pr_term syntax_const is_cons thm pat vars |
|
24219 | 547 |
and pr_bind' ((NONE, NONE), _) = str "_" |
548 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
549 |
| pr_bind' ((NONE, SOME p), _) = p |
|
550 |
| pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p] |
|
27304 | 551 |
and pr_bind thm = gen_pr_bind pr_bind' pr_term thm |
552 |
and pr_case thm vars fxy (cases as ((_, [_]), _)) = |
|
24219 | 553 |
let |
554 |
val (binds, t') = CodeThingol.unfold_let (ICase cases); |
|
555 |
fun pr ((pat, ty), t) vars = |
|
556 |
vars |
|
27304 | 557 |
|> pr_bind thm NOBR ((NONE, SOME pat), ty) |
558 |
|>> (fn p => semicolon [str "val", p, str "=", pr_term thm false vars NOBR t]) |
|
24219 | 559 |
val (ps, vars') = fold_map pr binds vars; |
560 |
in |
|
561 |
Pretty.chunks [ |
|
562 |
[str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block, |
|
27304 | 563 |
[str ("in"), Pretty.fbrk, pr_term thm false vars' NOBR t'] |> Pretty.block, |
24219 | 564 |
str ("end") |
565 |
] |
|
566 |
end |
|
27304 | 567 |
| pr_case thm vars fxy (((td, ty), b::bs), _) = |
24219 | 568 |
let |
569 |
fun pr delim (pat, t) = |
|
570 |
let |
|
27304 | 571 |
val (p, vars') = pr_bind thm NOBR ((NONE, SOME pat), ty) vars; |
24219 | 572 |
in |
27304 | 573 |
concat [str delim, p, str "=>", pr_term thm false vars' NOBR t] |
24219 | 574 |
end; |
575 |
in |
|
576 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
577 |
str "case" |
|
27304 | 578 |
:: pr_term thm false vars NOBR td |
24219 | 579 |
:: pr "of" b |
580 |
:: map (pr "|") bs |
|
581 |
) |
|
582 |
end |
|
27304 | 583 |
| pr_case thm vars fxy ((_, []), _) = str "raise Fail \"empty case\""; |
27103 | 584 |
fun pr_stmt (MLFuns (funns as (funn :: funns'))) = |
24219 | 585 |
let |
586 |
val definer = |
|
587 |
let |
|
27304 | 588 |
fun no_args _ (((ts, _), _) :: _) = length ts |
24841 | 589 |
| no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty; |
590 |
fun mk 0 [] = "val" |
|
24992 | 591 |
| mk 0 vs = if (null o filter_out (null o snd)) vs |
592 |
then "val" else "fun" |
|
24841 | 593 |
| mk k _ = "fun"; |
594 |
fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs) |
|
595 |
| chk (_, ((vs, ty), eqs)) (SOME defi) = |
|
596 |
if defi = mk (no_args ty eqs) vs then SOME defi |
|
24219 | 597 |
else error ("Mixing simultaneous vals and funs not implemented: " |
598 |
^ commas (map (labelled_name o fst) funns)); |
|
599 |
in the (fold chk funns NONE) end; |
|
27103 | 600 |
fun pr_funn definer (name, ((vs, ty), [])) = |
24219 | 601 |
let |
27103 | 602 |
val vs_dict = filter_out (null o snd) vs; |
603 |
val n = length vs_dict + (length o fst o CodeThingol.unfold_fun) ty; |
|
24992 | 604 |
val exc_str = |
605 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name; |
|
24219 | 606 |
in |
607 |
concat ( |
|
24841 | 608 |
str definer |
27103 | 609 |
:: (str o deresolve) name |
24841 | 610 |
:: map str (replicate n "_") |
611 |
@ str "=" |
|
612 |
:: str "raise" |
|
613 |
:: str "(Fail" |
|
27103 | 614 |
@@ str (exc_str ^ ")") |
24219 | 615 |
) |
616 |
end |
|
27103 | 617 |
| pr_funn definer (name, ((vs, ty), eqs as eq :: eqs')) = |
24841 | 618 |
let |
27103 | 619 |
val vs_dict = filter_out (null o snd) vs; |
24841 | 620 |
val shift = if null eqs' then I else |
621 |
map (Pretty.block o single o Pretty.block o single); |
|
27304 | 622 |
fun pr_eq definer ((ts, t), thm) = |
24841 | 623 |
let |
624 |
val consts = map_filter |
|
27103 | 625 |
(fn c => if (is_some o syntax_const) c |
626 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
24841 | 627 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
27103 | 628 |
val vars = reserved_names |
24841 | 629 |
|> CodeName.intro_vars consts |
630 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) |
|
631 |
(insert (op =)) ts []); |
|
632 |
in |
|
633 |
concat ( |
|
27103 | 634 |
[str definer, (str o deresolve) name] |
635 |
@ (if null ts andalso null vs_dict |
|
24841 | 636 |
then [str ":", pr_typ NOBR ty] |
637 |
else |
|
27103 | 638 |
pr_tyvar_dicts vs_dict |
27304 | 639 |
@ map (pr_term thm true vars BR) ts) |
640 |
@ [str "=", pr_term thm false vars NOBR t] |
|
24841 | 641 |
) |
642 |
end |
|
643 |
in |
|
644 |
(Pretty.block o Pretty.fbreaks o shift) ( |
|
645 |
pr_eq definer eq |
|
646 |
:: map (pr_eq "|") eqs' |
|
647 |
) |
|
648 |
end; |
|
24219 | 649 |
val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns'); |
650 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
27103 | 651 |
| pr_stmt (MLDatas (datas as (data :: datas'))) = |
24219 | 652 |
let |
653 |
fun pr_co (co, []) = |
|
27103 | 654 |
str (deresolve co) |
24219 | 655 |
| pr_co (co, tys) = |
656 |
concat [ |
|
27103 | 657 |
str (deresolve co), |
24219 | 658 |
str "of", |
659 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
|
660 |
]; |
|
661 |
fun pr_data definer (tyco, (vs, [])) = |
|
662 |
concat ( |
|
663 |
str definer |
|
664 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
665 |
:: str "=" |
|
666 |
@@ str "EMPTY__" |
|
667 |
) |
|
668 |
| pr_data definer (tyco, (vs, cos)) = |
|
669 |
concat ( |
|
670 |
str definer |
|
671 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
672 |
:: str "=" |
|
673 |
:: separate (str "|") (map pr_co cos) |
|
674 |
); |
|
24992 | 675 |
val (ps, p) = split_last |
676 |
(pr_data "datatype" data :: map (pr_data "and") datas'); |
|
24219 | 677 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
27103 | 678 |
| pr_stmt (MLClass (class, (v, (superclasses, classparams)))) = |
24219 | 679 |
let |
680 |
val w = first_upper v ^ "_"; |
|
681 |
fun pr_superclass_field (class, classrel) = |
|
682 |
(concat o map str) [ |
|
27103 | 683 |
pr_label_classrel classrel, ":", "'" ^ v, deresolve class |
24219 | 684 |
]; |
24841 | 685 |
fun pr_classparam_field (classparam, ty) = |
24219 | 686 |
concat [ |
24841 | 687 |
(str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty |
24219 | 688 |
]; |
24841 | 689 |
fun pr_classparam_proj (classparam, _) = |
24219 | 690 |
semicolon [ |
691 |
str "fun", |
|
27103 | 692 |
(str o deresolve) classparam, |
693 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)], |
|
24219 | 694 |
str "=", |
24841 | 695 |
str ("#" ^ pr_label_classparam classparam), |
24219 | 696 |
str w |
697 |
]; |
|
698 |
fun pr_superclass_proj (_, classrel) = |
|
699 |
semicolon [ |
|
700 |
str "fun", |
|
27103 | 701 |
(str o deresolve) classrel, |
702 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)], |
|
24219 | 703 |
str "=", |
704 |
str ("#" ^ pr_label_classrel classrel), |
|
705 |
str w |
|
706 |
]; |
|
707 |
in |
|
708 |
Pretty.chunks ( |
|
709 |
concat [ |
|
710 |
str ("type '" ^ v), |
|
27103 | 711 |
(str o deresolve) class, |
24219 | 712 |
str "=", |
713 |
Pretty.enum "," "{" "};" ( |
|
24841 | 714 |
map pr_superclass_field superclasses @ map pr_classparam_field classparams |
24219 | 715 |
) |
716 |
] |
|
717 |
:: map pr_superclass_proj superclasses |
|
24841 | 718 |
@ map pr_classparam_proj classparams |
24219 | 719 |
) |
720 |
end |
|
27103 | 721 |
| pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) = |
24219 | 722 |
let |
723 |
fun pr_superclass (_, (classrel, dss)) = |
|
724 |
concat [ |
|
725 |
(str o pr_label_classrel) classrel, |
|
726 |
str "=", |
|
727 |
pr_dicts NOBR [DictConst dss] |
|
728 |
]; |
|
27304 | 729 |
fun pr_classparam ((classparam, c_inst), thm) = |
24591 | 730 |
concat [ |
24841 | 731 |
(str o pr_label_classparam) classparam, |
24591 | 732 |
str "=", |
27304 | 733 |
pr_app thm false reserved_names NOBR (c_inst, []) |
24591 | 734 |
]; |
24219 | 735 |
in |
736 |
semicolon ([ |
|
737 |
str (if null arity then "val" else "fun"), |
|
27103 | 738 |
(str o deresolve) inst ] @ |
739 |
pr_tyvar_dicts arity @ [ |
|
24219 | 740 |
str "=", |
24992 | 741 |
Pretty.enum "," "{" "}" |
742 |
(map pr_superclass superarities @ map pr_classparam classparam_insts), |
|
24219 | 743 |
str ":", |
744 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
|
745 |
]) |
|
746 |
end; |
|
27103 | 747 |
in pr_stmt end; |
24219 | 748 |
|
27103 | 749 |
fun pr_sml_module name content = |
750 |
Pretty.chunks ( |
|
751 |
str ("structure " ^ name ^ " = ") |
|
752 |
:: str "struct" |
|
753 |
:: str "" |
|
754 |
:: content |
|
755 |
@ str "" |
|
756 |
@@ str ("end; (*struct " ^ name ^ "*)") |
|
757 |
); |
|
24219 | 758 |
|
27103 | 759 |
fun pr_ocaml_stmt syntax_tyco syntax_const labelled_name reserved_names deresolve is_cons = |
24219 | 760 |
let |
761 |
fun pr_dicts fxy ds = |
|
762 |
let |
|
763 |
fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v |
|
764 |
| pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1); |
|
765 |
fun pr_proj ps p = |
|
766 |
fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p |
|
27103 | 767 |
fun pr_dict fxy (DictConst (inst, dss)) = |
768 |
brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss) |
|
769 |
| pr_dict fxy (DictVar (classrels, v)) = |
|
770 |
pr_proj (map deresolve classrels) ((str o pr_dictvar) v) |
|
24219 | 771 |
in case ds |
772 |
of [] => str "()" |
|
27103 | 773 |
| [d] => pr_dict fxy d |
774 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds |
|
24219 | 775 |
end; |
27103 | 776 |
fun pr_tyvar_dicts vs = |
24219 | 777 |
vs |
24992 | 778 |
|> map (fn (v, sort) => map_index (fn (i, _) => |
779 |
DictVar ([], (v, (i, length sort)))) sort) |
|
24219 | 780 |
|> map (pr_dicts BR); |
781 |
fun pr_tycoexpr fxy (tyco, tys) = |
|
782 |
let |
|
27103 | 783 |
val tyco' = (str o deresolve) tyco |
24219 | 784 |
in case map (pr_typ BR) tys |
785 |
of [] => tyco' |
|
786 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
787 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
788 |
end |
|
27304 | 789 |
and pr_typ fxy (tyco `%% tys) = (case syntax_tyco tyco |
790 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
|
791 |
| SOME (i, pr) => pr pr_typ fxy tys) |
|
792 |
| pr_typ fxy (ITyVar v) = str ("'" ^ v); |
|
793 |
fun pr_term thm pat vars fxy (IConst c) = |
|
794 |
pr_app thm pat vars fxy (c, []) |
|
795 |
| pr_term thm pat vars fxy (IVar v) = |
|
24219 | 796 |
str (CodeName.lookup_var vars v) |
27304 | 797 |
| pr_term thm pat vars fxy (t as t1 `$ t2) = |
24219 | 798 |
(case CodeThingol.unfold_const_app t |
27304 | 799 |
of SOME c_ts => pr_app thm pat vars fxy c_ts |
24219 | 800 |
| NONE => |
27304 | 801 |
brackify fxy [pr_term thm pat vars NOBR t1, pr_term thm pat vars BR t2]) |
802 |
| pr_term thm pat vars fxy (t as _ `|-> _) = |
|
24219 | 803 |
let |
804 |
val (binds, t') = CodeThingol.unfold_abs t; |
|
27304 | 805 |
fun pr ((v, pat), ty) = pr_bind thm BR ((SOME v, pat), ty); |
24219 | 806 |
val (ps, vars') = fold_map pr binds vars; |
27304 | 807 |
in brackets (str "fun" :: ps @ str "->" @@ pr_term thm pat vars' NOBR t') end |
808 |
| pr_term thm pat vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0 |
|
27103 | 809 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
27304 | 810 |
then pr_case thm vars fxy cases |
811 |
else pr_app thm pat vars fxy c_ts |
|
812 |
| NONE => pr_case thm vars fxy cases) |
|
813 |
and pr_app' thm pat vars (app as ((c, (iss, tys)), ts)) = |
|
24219 | 814 |
if is_cons c then |
815 |
if length tys = length ts |
|
816 |
then case ts |
|
27103 | 817 |
of [] => [(str o deresolve) c] |
27304 | 818 |
| [t] => [(str o deresolve) c, pr_term thm pat vars BR t] |
27103 | 819 |
| _ => [(str o deresolve) c, Pretty.enum "," "(" ")" |
27304 | 820 |
(map (pr_term thm pat vars NOBR) ts)] |
27710 | 821 |
else [pr_term thm pat vars BR (CodeThingol.eta_expand (length tys) app)] |
27103 | 822 |
else (str o deresolve) c |
27304 | 823 |
:: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term thm pat vars BR) ts) |
824 |
and pr_app thm pat vars = gen_pr_app pr_app' pr_term syntax_const is_cons thm pat vars |
|
24219 | 825 |
and pr_bind' ((NONE, NONE), _) = str "_" |
826 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
827 |
| pr_bind' ((NONE, SOME p), _) = p |
|
828 |
| pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v] |
|
27304 | 829 |
and pr_bind thm = gen_pr_bind pr_bind' pr_term thm |
830 |
and pr_case thm vars fxy (cases as ((_, [_]), _)) = |
|
24219 | 831 |
let |
832 |
val (binds, t') = CodeThingol.unfold_let (ICase cases); |
|
833 |
fun pr ((pat, ty), t) vars = |
|
834 |
vars |
|
27304 | 835 |
|> pr_bind thm NOBR ((NONE, SOME pat), ty) |
24992 | 836 |
|>> (fn p => concat |
27304 | 837 |
[str "let", p, str "=", pr_term thm false vars NOBR t, str "in"]) |
24219 | 838 |
val (ps, vars') = fold_map pr binds vars; |
27304 | 839 |
in Pretty.chunks (ps @| pr_term thm false vars' NOBR t') end |
840 |
| pr_case thm vars fxy (((td, ty), b::bs), _) = |
|
24219 | 841 |
let |
842 |
fun pr delim (pat, t) = |
|
843 |
let |
|
27304 | 844 |
val (p, vars') = pr_bind thm NOBR ((NONE, SOME pat), ty) vars; |
845 |
in concat [str delim, p, str "->", pr_term thm false vars' NOBR t] end; |
|
24219 | 846 |
in |
847 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
848 |
str "match" |
|
27304 | 849 |
:: pr_term thm false vars NOBR td |
24219 | 850 |
:: pr "with" b |
851 |
:: map (pr "|") bs |
|
852 |
) |
|
853 |
end |
|
27304 | 854 |
| pr_case thm vars fxy ((_, []), _) = str "failwith \"empty case\""; |
27103 | 855 |
fun fish_params vars eqs = |
856 |
let |
|
857 |
fun fish_param _ (w as SOME _) = w |
|
858 |
| fish_param (IVar v) NONE = SOME v |
|
859 |
| fish_param _ NONE = NONE; |
|
860 |
fun fillup_param _ (_, SOME v) = v |
|
861 |
| fillup_param x (i, NONE) = x ^ string_of_int i; |
|
862 |
val fished1 = fold (map2 fish_param) eqs (replicate (length (hd eqs)) NONE); |
|
863 |
val x = Name.variant (map_filter I fished1) "x"; |
|
864 |
val fished2 = map_index (fillup_param x) fished1; |
|
865 |
val (fished3, _) = Name.variants fished2 Name.context; |
|
866 |
val vars' = CodeName.intro_vars fished3 vars; |
|
867 |
in map (CodeName.lookup_var vars') fished3 end; |
|
868 |
fun pr_stmt (MLFuns (funns as funn :: funns')) = |
|
24219 | 869 |
let |
27304 | 870 |
fun pr_eq ((ts, t), thm) = |
24219 | 871 |
let |
872 |
val consts = map_filter |
|
27103 | 873 |
(fn c => if (is_some o syntax_const) c |
874 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
24219 | 875 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
27103 | 876 |
val vars = reserved_names |
24219 | 877 |
|> CodeName.intro_vars consts |
878 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) |
|
879 |
(insert (op =)) ts []); |
|
880 |
in concat [ |
|
27304 | 881 |
(Pretty.block o Pretty.commas) (map (pr_term thm true vars NOBR) ts), |
24219 | 882 |
str "->", |
27304 | 883 |
pr_term thm false vars NOBR t |
24219 | 884 |
] end; |
24841 | 885 |
fun pr_eqs name ty [] = |
886 |
let |
|
887 |
val n = (length o fst o CodeThingol.unfold_fun) ty; |
|
24992 | 888 |
val exc_str = |
889 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name; |
|
24841 | 890 |
in |
891 |
concat ( |
|
892 |
map str (replicate n "_") |
|
893 |
@ str "=" |
|
894 |
:: str "failwith" |
|
24992 | 895 |
@@ str exc_str |
24841 | 896 |
) |
897 |
end |
|
27304 | 898 |
| pr_eqs _ _ [((ts, t), thm)] = |
24219 | 899 |
let |
900 |
val consts = map_filter |
|
27103 | 901 |
(fn c => if (is_some o syntax_const) c |
902 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
24219 | 903 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
27103 | 904 |
val vars = reserved_names |
24219 | 905 |
|> CodeName.intro_vars consts |
906 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) |
|
907 |
(insert (op =)) ts []); |
|
908 |
in |
|
909 |
concat ( |
|
27304 | 910 |
map (pr_term thm true vars BR) ts |
24219 | 911 |
@ str "=" |
27304 | 912 |
@@ pr_term thm false vars NOBR t |
24219 | 913 |
) |
914 |
end |
|
27304 | 915 |
| pr_eqs _ _ (eqs as (eq as (([_], _), _)) :: eqs') = |
24219 | 916 |
Pretty.block ( |
917 |
str "=" |
|
918 |
:: Pretty.brk 1 |
|
919 |
:: str "function" |
|
920 |
:: Pretty.brk 1 |
|
921 |
:: pr_eq eq |
|
24992 | 922 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
923 |
o single o pr_eq) eqs' |
|
24219 | 924 |
) |
24841 | 925 |
| pr_eqs _ _ (eqs as eq :: eqs') = |
24219 | 926 |
let |
927 |
val consts = map_filter |
|
27103 | 928 |
(fn c => if (is_some o syntax_const) c |
929 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
24992 | 930 |
((fold o CodeThingol.fold_constnames) |
27304 | 931 |
(insert (op =)) (map (snd o fst) eqs) []); |
27103 | 932 |
val vars = reserved_names |
24219 | 933 |
|> CodeName.intro_vars consts; |
27304 | 934 |
val dummy_parms = (map str o fish_params vars o map (fst o fst)) eqs; |
24219 | 935 |
in |
936 |
Pretty.block ( |
|
937 |
Pretty.breaks dummy_parms |
|
938 |
@ Pretty.brk 1 |
|
939 |
:: str "=" |
|
940 |
:: Pretty.brk 1 |
|
941 |
:: str "match" |
|
942 |
:: Pretty.brk 1 |
|
943 |
:: (Pretty.block o Pretty.commas) dummy_parms |
|
944 |
:: Pretty.brk 1 |
|
945 |
:: str "with" |
|
946 |
:: Pretty.brk 1 |
|
947 |
:: pr_eq eq |
|
24992 | 948 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
949 |
o single o pr_eq) eqs' |
|
24219 | 950 |
) |
951 |
end; |
|
24381 | 952 |
fun pr_funn definer (name, ((vs, ty), eqs)) = |
24219 | 953 |
concat ( |
954 |
str definer |
|
27103 | 955 |
:: (str o deresolve) name |
956 |
:: pr_tyvar_dicts (filter_out (null o snd) vs) |
|
24841 | 957 |
@| pr_eqs name ty eqs |
24219 | 958 |
); |
24992 | 959 |
val (ps, p) = split_last |
960 |
(pr_funn "let rec" funn :: map (pr_funn "and") funns'); |
|
24219 | 961 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
27103 | 962 |
| pr_stmt (MLDatas (datas as (data :: datas'))) = |
24219 | 963 |
let |
964 |
fun pr_co (co, []) = |
|
27103 | 965 |
str (deresolve co) |
24219 | 966 |
| pr_co (co, tys) = |
967 |
concat [ |
|
27103 | 968 |
str (deresolve co), |
24219 | 969 |
str "of", |
970 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
|
971 |
]; |
|
972 |
fun pr_data definer (tyco, (vs, [])) = |
|
973 |
concat ( |
|
974 |
str definer |
|
975 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
976 |
:: str "=" |
|
977 |
@@ str "EMPTY_" |
|
978 |
) |
|
979 |
| pr_data definer (tyco, (vs, cos)) = |
|
980 |
concat ( |
|
981 |
str definer |
|
982 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
983 |
:: str "=" |
|
984 |
:: separate (str "|") (map pr_co cos) |
|
985 |
); |
|
24992 | 986 |
val (ps, p) = split_last |
987 |
(pr_data "type" data :: map (pr_data "and") datas'); |
|
24219 | 988 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
27103 | 989 |
| pr_stmt (MLClass (class, (v, (superclasses, classparams)))) = |
24219 | 990 |
let |
991 |
val w = "_" ^ first_upper v; |
|
992 |
fun pr_superclass_field (class, classrel) = |
|
993 |
(concat o map str) [ |
|
27103 | 994 |
deresolve classrel, ":", "'" ^ v, deresolve class |
24219 | 995 |
]; |
24841 | 996 |
fun pr_classparam_field (classparam, ty) = |
24219 | 997 |
concat [ |
27103 | 998 |
(str o deresolve) classparam, str ":", pr_typ NOBR ty |
24219 | 999 |
]; |
24841 | 1000 |
fun pr_classparam_proj (classparam, _) = |
24219 | 1001 |
concat [ |
1002 |
str "let", |
|
27103 | 1003 |
(str o deresolve) classparam, |
24219 | 1004 |
str w, |
1005 |
str "=", |
|
27103 | 1006 |
str (w ^ "." ^ deresolve classparam ^ ";;") |
24219 | 1007 |
]; |
1008 |
in Pretty.chunks ( |
|
1009 |
concat [ |
|
1010 |
str ("type '" ^ v), |
|
27103 | 1011 |
(str o deresolve) class, |
24219 | 1012 |
str "=", |
25771 | 1013 |
enum_default "();;" ";" "{" "};;" ( |
24992 | 1014 |
map pr_superclass_field superclasses |
1015 |
@ map pr_classparam_field classparams |
|
24219 | 1016 |
) |
1017 |
] |
|
24841 | 1018 |
:: map pr_classparam_proj classparams |
24219 | 1019 |
) end |
27103 | 1020 |
| pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) = |
24219 | 1021 |
let |
1022 |
fun pr_superclass (_, (classrel, dss)) = |
|
1023 |
concat [ |
|
27103 | 1024 |
(str o deresolve) classrel, |
24219 | 1025 |
str "=", |
1026 |
pr_dicts NOBR [DictConst dss] |
|
1027 |
]; |
|
27304 | 1028 |
fun pr_classparam_inst ((classparam, c_inst), thm) = |
24591 | 1029 |
concat [ |
27103 | 1030 |
(str o deresolve) classparam, |
24591 | 1031 |
str "=", |
27304 | 1032 |
pr_app thm false reserved_names NOBR (c_inst, []) |
24591 | 1033 |
]; |
24219 | 1034 |
in |
1035 |
concat ( |
|
1036 |
str "let" |
|
27103 | 1037 |
:: (str o deresolve) inst |
1038 |
:: pr_tyvar_dicts arity |
|
24219 | 1039 |
@ str "=" |
1040 |
@@ (Pretty.enclose "(" ");;" o Pretty.breaks) [ |
|
25771 | 1041 |
enum_default "()" ";" "{" "}" (map pr_superclass superarities |
24992 | 1042 |
@ map pr_classparam_inst classparam_insts), |
24219 | 1043 |
str ":", |
1044 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
|
1045 |
] |
|
1046 |
) |
|
1047 |
end; |
|
27103 | 1048 |
in pr_stmt end; |
1049 |
||
1050 |
fun pr_ocaml_module name content = |
|
1051 |
Pretty.chunks ( |
|
1052 |
str ("module " ^ name ^ " = ") |
|
1053 |
:: str "struct" |
|
1054 |
:: str "" |
|
1055 |
:: content |
|
1056 |
@ str "" |
|
1057 |
@@ str ("end;; (*struct " ^ name ^ "*)") |
|
1058 |
); |
|
24219 | 1059 |
|
27103 | 1060 |
local |
1061 |
||
1062 |
datatype ml_node = |
|
1063 |
Dummy of string |
|
1064 |
| Stmt of string * ml_stmt |
|
1065 |
| Module of string * ((Name.context * Name.context) * ml_node Graph.T); |
|
24219 | 1066 |
|
27103 | 1067 |
in |
1068 |
||
1069 |
fun ml_node_of_program labelled_name module_name reserved_names raw_module_alias program = |
|
24219 | 1070 |
let |
27103 | 1071 |
val module_alias = if is_some module_name then K module_name else raw_module_alias; |
1072 |
val reserved_names = Name.make_context reserved_names; |
|
1073 |
val empty_module = ((reserved_names, reserved_names), Graph.empty); |
|
24219 | 1074 |
fun map_node [] f = f |
1075 |
| map_node (m::ms) f = |
|
27103 | 1076 |
Graph.default_node (m, Module (m, empty_module)) |
1077 |
#> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) => |
|
1078 |
Module (module_name, (nsp, map_node ms f nodes))); |
|
24219 | 1079 |
fun map_nsp_yield [] f (nsp, nodes) = |
1080 |
let |
|
1081 |
val (x, nsp') = f nsp |
|
1082 |
in (x, (nsp', nodes)) end |
|
1083 |
| map_nsp_yield (m::ms) f (nsp, nodes) = |
|
1084 |
let |
|
1085 |
val (x, nodes') = |
|
1086 |
nodes |
|
27103 | 1087 |
|> Graph.default_node (m, Module (m, empty_module)) |
1088 |
|> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => |
|
24219 | 1089 |
let |
1090 |
val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes |
|
27103 | 1091 |
in (x, Module (d_module_name, nsp_nodes')) end) |
24219 | 1092 |
in (x, (nsp, nodes')) end; |
27103 | 1093 |
fun map_nsp_fun_yield f (nsp_fun, nsp_typ) = |
1094 |
let |
|
1095 |
val (x, nsp_fun') = f nsp_fun |
|
1096 |
in (x, (nsp_fun', nsp_typ)) end; |
|
1097 |
fun map_nsp_typ_yield f (nsp_fun, nsp_typ) = |
|
1098 |
let |
|
1099 |
val (x, nsp_typ') = f nsp_typ |
|
1100 |
in (x, (nsp_fun, nsp_typ')) end; |
|
1101 |
val mk_name_module = mk_name_module reserved_names NONE module_alias program; |
|
1102 |
fun mk_name_stmt upper name nsp = |
|
24219 | 1103 |
let |
1104 |
val (_, base) = dest_name name; |
|
1105 |
val base' = if upper then first_upper base else base; |
|
1106 |
val ([base''], nsp') = Name.variants [base'] nsp; |
|
1107 |
in (base'', nsp') end; |
|
27103 | 1108 |
fun add_funs stmts = |
24219 | 1109 |
fold_map |
27103 | 1110 |
(fn (name, CodeThingol.Fun stmt) => |
1111 |
map_nsp_fun_yield (mk_name_stmt false name) #>> |
|
27304 | 1112 |
rpair (name, stmt) |
27103 | 1113 |
| (name, _) => |
27024 | 1114 |
error ("Function block containing illegal statement: " ^ labelled_name name) |
27103 | 1115 |
) stmts |
1116 |
#>> (split_list #> apsnd MLFuns); |
|
1117 |
fun add_datatypes stmts = |
|
24219 | 1118 |
fold_map |
27103 | 1119 |
(fn (name, CodeThingol.Datatype stmt) => |
1120 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt)) |
|
24219 | 1121 |
| (name, CodeThingol.Datatypecons _) => |
27103 | 1122 |
map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE |
1123 |
| (name, _) => |
|
27024 | 1124 |
error ("Datatype block containing illegal statement: " ^ labelled_name name) |
27103 | 1125 |
) stmts |
1126 |
#>> (split_list #> apsnd (map_filter I |
|
27024 | 1127 |
#> (fn [] => error ("Datatype block without data statement: " |
27103 | 1128 |
^ (commas o map (labelled_name o fst)) stmts) |
1129 |
| stmts => MLDatas stmts))); |
|
1130 |
fun add_class stmts = |
|
24219 | 1131 |
fold_map |
1132 |
(fn (name, CodeThingol.Class info) => |
|
27103 | 1133 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, info)) |
24219 | 1134 |
| (name, CodeThingol.Classrel _) => |
27103 | 1135 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
24841 | 1136 |
| (name, CodeThingol.Classparam _) => |
27103 | 1137 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
1138 |
| (name, _) => |
|
27024 | 1139 |
error ("Class block containing illegal statement: " ^ labelled_name name) |
27103 | 1140 |
) stmts |
1141 |
#>> (split_list #> apsnd (map_filter I |
|
27024 | 1142 |
#> (fn [] => error ("Class block without class statement: " |
27103 | 1143 |
^ (commas o map (labelled_name o fst)) stmts) |
1144 |
| [stmt] => MLClass stmt))); |
|
1145 |
fun add_inst [(name, CodeThingol.Classinst stmt)] = |
|
1146 |
map_nsp_fun_yield (mk_name_stmt false name) |
|
27304 | 1147 |
#>> (fn base => ([base], MLClassinst (name, stmt))); |
27103 | 1148 |
fun add_stmts ((stmts as (_, CodeThingol.Fun _)::_)) = |
1149 |
add_funs stmts |
|
1150 |
| add_stmts ((stmts as (_, CodeThingol.Datatypecons _)::_)) = |
|
1151 |
add_datatypes stmts |
|
1152 |
| add_stmts ((stmts as (_, CodeThingol.Datatype _)::_)) = |
|
1153 |
add_datatypes stmts |
|
1154 |
| add_stmts ((stmts as (_, CodeThingol.Class _)::_)) = |
|
1155 |
add_class stmts |
|
1156 |
| add_stmts ((stmts as (_, CodeThingol.Classrel _)::_)) = |
|
1157 |
add_class stmts |
|
1158 |
| add_stmts ((stmts as (_, CodeThingol.Classparam _)::_)) = |
|
1159 |
add_class stmts |
|
1160 |
| add_stmts ((stmts as [(_, CodeThingol.Classinst _)])) = |
|
1161 |
add_inst stmts |
|
1162 |
| add_stmts stmts = error ("Illegal mutual dependencies: " ^ |
|
1163 |
(commas o map (labelled_name o fst)) stmts); |
|
1164 |
fun add_stmts' stmts nsp_nodes = |
|
24219 | 1165 |
let |
27103 | 1166 |
val names as (name :: names') = map fst stmts; |
24219 | 1167 |
val deps = |
1168 |
[] |
|
27103 | 1169 |
|> fold (fold (insert (op =)) o Graph.imm_succs program) names |
24219 | 1170 |
|> subtract (op =) names; |
27103 | 1171 |
val (module_names, _) = (split_list o map dest_name) names; |
1172 |
val module_name = (the_single o distinct (op =) o map mk_name_module) module_names |
|
24219 | 1173 |
handle Empty => |
24811 | 1174 |
error ("Different namespace prefixes for mutual dependencies:\n" |
1175 |
^ commas (map labelled_name names) |
|
1176 |
^ "\n" |
|
27103 | 1177 |
^ commas module_names); |
1178 |
val module_name_path = NameSpace.explode module_name; |
|
1179 |
fun add_dep name name' = |
|
24219 | 1180 |
let |
27103 | 1181 |
val module_name' = (mk_name_module o fst o dest_name) name'; |
1182 |
in if module_name = module_name' then |
|
1183 |
map_node module_name_path (Graph.add_edge (name, name')) |
|
24219 | 1184 |
else let |
1185 |
val (common, (diff1::_, diff2::_)) = chop_prefix (op =) |
|
27103 | 1186 |
(module_name_path, NameSpace.explode module_name'); |
24219 | 1187 |
in |
1188 |
map_node common |
|
27103 | 1189 |
(fn node => Graph.add_edge_acyclic (diff1, diff2) node |
24219 | 1190 |
handle Graph.CYCLES _ => error ("Dependency " |
27103 | 1191 |
^ quote name ^ " -> " ^ quote name' |
24219 | 1192 |
^ " would result in module dependency cycle")) |
1193 |
end end; |
|
1194 |
in |
|
1195 |
nsp_nodes |
|
27103 | 1196 |
|> map_nsp_yield module_name_path (add_stmts stmts) |
1197 |
|-> (fn (base' :: bases', stmt') => |
|
1198 |
apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt'))) |
|
24992 | 1199 |
#> fold2 (fn name' => fn base' => |
27103 | 1200 |
Graph.new_node (name', (Dummy base'))) names' bases'))) |
24219 | 1201 |
|> apsnd (fold (fn name => fold (add_dep name) deps) names) |
27103 | 1202 |
|> apsnd (fold_product (curry (map_node module_name_path o Graph.add_edge)) names names) |
24219 | 1203 |
end; |
27103 | 1204 |
val (_, nodes) = empty_module |
1205 |
|> fold add_stmts' (map (AList.make (Graph.get_node program)) |
|
1206 |
(rev (Graph.strong_conn program))); |
|
24219 | 1207 |
fun deresolver prefix name = |
1208 |
let |
|
27103 | 1209 |
val module_name = (fst o dest_name) name; |
1210 |
val module_name' = (NameSpace.explode o mk_name_module) module_name; |
|
1211 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name'); |
|
1212 |
val stmt_name = |
|
24219 | 1213 |
nodes |
27103 | 1214 |
|> fold (fn name => fn node => case Graph.get_node node name |
1215 |
of Module (_, (_, node)) => node) module_name' |
|
1216 |
|> (fn node => case Graph.get_node node name of Stmt (stmt_name, _) => stmt_name |
|
1217 |
| Dummy stmt_name => stmt_name); |
|
24219 | 1218 |
in |
27103 | 1219 |
NameSpace.implode (remainder @ [stmt_name]) |
24219 | 1220 |
end handle Graph.UNDEF _ => |
27024 | 1221 |
error ("Unknown statement name: " ^ labelled_name name); |
27103 | 1222 |
in (deresolver, nodes) end; |
1223 |
||
27436 | 1224 |
fun serialize_ml compile pr_module pr_stmt raw_module_name labelled_name reserved_names includes raw_module_alias |
27103 | 1225 |
_ syntax_tyco syntax_const program cs destination = |
1226 |
let |
|
1227 |
val is_cons = CodeThingol.is_cons program; |
|
27304 | 1228 |
val stmt_names = stmt_names_of_destination destination; |
1229 |
val module_name = if null stmt_names then raw_module_name else SOME "Code"; |
|
27103 | 1230 |
val (deresolver, nodes) = ml_node_of_program labelled_name module_name |
1231 |
reserved_names raw_module_alias program; |
|
1232 |
val reserved_names = CodeName.make_vars reserved_names; |
|
1233 |
fun pr_node prefix (Dummy _) = |
|
24219 | 1234 |
NONE |
27304 | 1235 |
| pr_node prefix (Stmt (_, stmt)) = if null stmt_names orelse |
1236 |
(not o null o filter (member (op =) stmt_names) o stmt_names_of) stmt then SOME |
|
1237 |
(pr_stmt syntax_tyco syntax_const labelled_name reserved_names |
|
1238 |
(deresolver prefix) is_cons stmt) |
|
1239 |
else NONE |
|
27103 | 1240 |
| pr_node prefix (Module (module_name, (_, nodes))) = |
27304 | 1241 |
let |
1242 |
val ps = separate (str "") |
|
1243 |
((map_filter (pr_node (prefix @ [module_name]) o Graph.get_node nodes) |
|
1244 |
o rev o flat o Graph.strong_conn) nodes) |
|
1245 |
in SOME (case destination of String _ => Pretty.chunks ps |
|
1246 |
| _ => pr_module module_name ps) |
|
1247 |
end; |
|
27103 | 1248 |
val cs' = map_filter (try (deresolver (if is_some module_name then the_list module_name else []))) |
1249 |
cs; |
|
24992 | 1250 |
val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter |
26113 | 1251 |
(pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)); |
27103 | 1252 |
fun output Compile = K NONE o compile o code_of_pretty |
27304 | 1253 |
| output Export = K NONE o code_writeln |
27103 | 1254 |
| output (File file) = K NONE o File.write file o code_of_pretty |
27436 | 1255 |
| output (String _) = SOME o rpair cs' o code_of_pretty; |
1256 |
in output destination p end; |
|
27103 | 1257 |
|
1258 |
end; (*local*) |
|
24219 | 1259 |
|
27436 | 1260 |
(* ML (system language) code for evaluation and instrumentalization *) |
1261 |
||
1262 |
fun ml_code_of thy program cs = mount_serializer thy |
|
1263 |
(SOME (fn _ => fn [] => serialize_ml (K ()) (K Pretty.chunks) pr_sml_stmt (SOME ""))) |
|
1264 |
target_SML NONE [] program cs (String []) |
|
1265 |
|> the; |
|
1266 |
||
1267 |
(* generic entry points for SML/OCaml *) |
|
1268 |
||
27103 | 1269 |
fun isar_seri_sml module_name = |
27000 | 1270 |
parse_args (Scan.succeed ()) |
27103 | 1271 |
#> (fn () => serialize_ml (use_text (1, "generated code") Output.ml_output false) |
27436 | 1272 |
pr_sml_module pr_sml_stmt module_name); |
26113 | 1273 |
|
27103 | 1274 |
fun isar_seri_ocaml module_name = |
27000 | 1275 |
parse_args (Scan.succeed ()) |
27103 | 1276 |
#> (fn () => serialize_ml (fn _ => error "OCaml: no internal compilation") |
27436 | 1277 |
pr_ocaml_module pr_ocaml_stmt module_name); |
24219 | 1278 |
|
1279 |
||
1280 |
(** Haskell serializer **) |
|
1281 |
||
27304 | 1282 |
fun pr_haskell_bind pr_term = |
27103 | 1283 |
let |
1284 |
fun pr_bind ((NONE, NONE), _) = str "_" |
|
1285 |
| pr_bind ((SOME v, NONE), _) = str v |
|
1286 |
| pr_bind ((NONE, SOME p), _) = p |
|
1287 |
| pr_bind ((SOME v, SOME p), _) = brackets [str v, str "@", p]; |
|
27304 | 1288 |
in gen_pr_bind pr_bind pr_term end; |
24219 | 1289 |
|
27103 | 1290 |
fun pr_haskell_stmt syntax_class syntax_tyco syntax_const labelled_name |
1291 |
init_syms deresolve is_cons contr_classparam_typs deriving_show = |
|
24219 | 1292 |
let |
27103 | 1293 |
val deresolve_base = NameSpace.base o deresolve; |
1294 |
fun class_name class = case syntax_class class |
|
1295 |
of NONE => deresolve class |
|
24219 | 1296 |
| SOME (class, _) => class; |
27103 | 1297 |
fun classparam_name class classparam = case syntax_class class |
1298 |
of NONE => deresolve_base classparam |
|
24841 | 1299 |
| SOME (_, classparam_syntax) => case classparam_syntax classparam |
1300 |
of NONE => (snd o dest_name) classparam |
|
25621 | 1301 |
| SOME classparam => classparam; |
1302 |
fun pr_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs |
|
1303 |
of [] => [] |
|
1304 |
| classbinds => Pretty.enum "," "(" ")" ( |
|
1305 |
map (fn (v, class) => |
|
1306 |
str (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) classbinds) |
|
1307 |
@@ str " => "; |
|
1308 |
fun pr_typforall tyvars vs = case map fst vs |
|
1309 |
of [] => [] |
|
1310 |
| vnames => str "forall " :: Pretty.breaks |
|
1311 |
(map (str o CodeName.lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1; |
|
24219 | 1312 |
fun pr_tycoexpr tyvars fxy (tyco, tys) = |
1313 |
brackify fxy (str tyco :: map (pr_typ tyvars BR) tys) |
|
27304 | 1314 |
and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) = (case syntax_tyco tyco |
1315 |
of NONE => pr_tycoexpr tyvars fxy (deresolve tyco, tys) |
|
1316 |
| SOME (i, pr) => pr (pr_typ tyvars) fxy tys) |
|
1317 |
| pr_typ tyvars fxy (ITyVar v) = (str o CodeName.lookup_var tyvars) v; |
|
25621 | 1318 |
fun pr_typdecl tyvars (vs, tycoexpr) = |
1319 |
Pretty.block (pr_typcontext tyvars vs @| pr_tycoexpr tyvars NOBR tycoexpr); |
|
24219 | 1320 |
fun pr_typscheme tyvars (vs, ty) = |
25621 | 1321 |
Pretty.block (pr_typforall tyvars vs @ pr_typcontext tyvars vs @| pr_typ tyvars NOBR ty); |
27304 | 1322 |
fun pr_term tyvars thm pat vars fxy (IConst c) = |
1323 |
pr_app tyvars thm pat vars fxy (c, []) |
|
1324 |
| pr_term tyvars thm pat vars fxy (t as (t1 `$ t2)) = |
|
24219 | 1325 |
(case CodeThingol.unfold_const_app t |
27304 | 1326 |
of SOME app => pr_app tyvars thm pat vars fxy app |
24219 | 1327 |
| _ => |
1328 |
brackify fxy [ |
|
27304 | 1329 |
pr_term tyvars thm pat vars NOBR t1, |
1330 |
pr_term tyvars thm pat vars BR t2 |
|
24219 | 1331 |
]) |
27304 | 1332 |
| pr_term tyvars thm pat vars fxy (IVar v) = |
24219 | 1333 |
(str o CodeName.lookup_var vars) v |
27304 | 1334 |
| pr_term tyvars thm pat vars fxy (t as _ `|-> _) = |
24219 | 1335 |
let |
1336 |
val (binds, t') = CodeThingol.unfold_abs t; |
|
27304 | 1337 |
fun pr ((v, pat), ty) = pr_bind tyvars thm BR ((SOME v, pat), ty); |
24219 | 1338 |
val (ps, vars') = fold_map pr binds vars; |
27304 | 1339 |
in brackets (str "\\" :: ps @ str "->" @@ pr_term tyvars thm pat vars' NOBR t') end |
1340 |
| pr_term tyvars thm pat vars fxy (ICase (cases as (_, t0))) = |
|
24992 | 1341 |
(case CodeThingol.unfold_const_app t0 |
27103 | 1342 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
27304 | 1343 |
then pr_case tyvars thm vars fxy cases |
1344 |
else pr_app tyvars thm pat vars fxy c_ts |
|
1345 |
| NONE => pr_case tyvars thm vars fxy cases) |
|
1346 |
and pr_app' tyvars thm pat vars ((c, (_, tys)), ts) = case contr_classparam_typs c |
|
1347 |
of [] => (str o deresolve) c :: map (pr_term tyvars thm pat vars BR) ts |
|
25621 | 1348 |
| fingerprint => let |
1349 |
val ts_fingerprint = ts ~~ curry Library.take (length ts) fingerprint; |
|
1350 |
val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) => |
|
1351 |
(not o CodeThingol.locally_monomorphic) t) ts_fingerprint; |
|
27304 | 1352 |
fun pr_term_anno (t, NONE) _ = pr_term tyvars thm pat vars BR t |
25621 | 1353 |
| pr_term_anno (t, SOME _) ty = |
27304 | 1354 |
brackets [pr_term tyvars thm pat vars NOBR t, str "::", pr_typ tyvars NOBR ty]; |
25621 | 1355 |
in |
1356 |
if needs_annotation then |
|
27103 | 1357 |
(str o deresolve) c :: map2 pr_term_anno ts_fingerprint (curry Library.take (length ts) tys) |
27304 | 1358 |
else (str o deresolve) c :: map (pr_term tyvars thm pat vars BR) ts |
25621 | 1359 |
end |
27304 | 1360 |
and pr_app tyvars = gen_pr_app (pr_app' tyvars) (pr_term tyvars) syntax_const is_cons |
27103 | 1361 |
and pr_bind tyvars = pr_haskell_bind (pr_term tyvars) |
27304 | 1362 |
and pr_case tyvars thm vars fxy (cases as ((_, [_]), _)) = |
24219 | 1363 |
let |
1364 |
val (binds, t) = CodeThingol.unfold_let (ICase cases); |
|
1365 |
fun pr ((pat, ty), t) vars = |
|
1366 |
vars |
|
27304 | 1367 |
|> pr_bind tyvars thm BR ((NONE, SOME pat), ty) |
1368 |
|>> (fn p => semicolon [p, str "=", pr_term tyvars thm false vars NOBR t]) |
|
24219 | 1369 |
val (ps, vars') = fold_map pr binds vars; |
1370 |
in |
|
1371 |
Pretty.block_enclose ( |
|
1372 |
str "let {", |
|
27304 | 1373 |
concat [str "}", str "in", pr_term tyvars thm false vars' NOBR t] |
24219 | 1374 |
) ps |
1375 |
end |
|
27304 | 1376 |
| pr_case tyvars thm vars fxy (((td, ty), bs as _ :: _), _) = |
24219 | 1377 |
let |
1378 |
fun pr (pat, t) = |
|
1379 |
let |
|
27304 | 1380 |
val (p, vars') = pr_bind tyvars thm NOBR ((NONE, SOME pat), ty) vars; |
1381 |
in semicolon [p, str "->", pr_term tyvars thm false vars' NOBR t] end; |
|
24219 | 1382 |
in |
1383 |
Pretty.block_enclose ( |
|
27304 | 1384 |
concat [str "(case", pr_term tyvars thm false vars NOBR td, str "of", str "{"], |
24219 | 1385 |
str "})" |
1386 |
) (map pr bs) |
|
1387 |
end |
|
27304 | 1388 |
| pr_case tyvars thm vars fxy ((_, []), _) = str "error \"empty case\""; |
27103 | 1389 |
fun pr_stmt (name, CodeThingol.Fun ((vs, ty), [])) = |
24841 | 1390 |
let |
1391 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
1392 |
val n = (length o fst o CodeThingol.unfold_fun) ty; |
|
1393 |
in |
|
1394 |
Pretty.chunks [ |
|
1395 |
Pretty.block [ |
|
27103 | 1396 |
(str o suffix " ::" o deresolve_base) name, |
24841 | 1397 |
Pretty.brk 1, |
1398 |
pr_typscheme tyvars (vs, ty), |
|
1399 |
str ";" |
|
1400 |
], |
|
1401 |
concat ( |
|
27103 | 1402 |
(str o deresolve_base) name |
24841 | 1403 |
:: map str (replicate n "_") |
1404 |
@ str "=" |
|
1405 |
:: str "error" |
|
24992 | 1406 |
@@ (str o (fn s => s ^ ";") o ML_Syntax.print_string |
1407 |
o NameSpace.base o NameSpace.qualifier) name |
|
24841 | 1408 |
) |
1409 |
] |
|
1410 |
end |
|
27103 | 1411 |
| pr_stmt (name, CodeThingol.Fun ((vs, ty), eqs)) = |
24219 | 1412 |
let |
1413 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
27304 | 1414 |
fun pr_eq ((ts, t), thm) = |
24219 | 1415 |
let |
1416 |
val consts = map_filter |
|
27103 | 1417 |
(fn c => if (is_some o syntax_const) c |
1418 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
24219 | 1419 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
1420 |
val vars = init_syms |
|
1421 |
|> CodeName.intro_vars consts |
|
1422 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) |
|
1423 |
(insert (op =)) ts []); |
|
1424 |
in |
|
1425 |
semicolon ( |
|
27103 | 1426 |
(str o deresolve_base) name |
27304 | 1427 |
:: map (pr_term tyvars thm true vars BR) ts |
24219 | 1428 |
@ str "=" |
27304 | 1429 |
@@ pr_term tyvars thm false vars NOBR t |
24219 | 1430 |
) |
1431 |
end; |
|
1432 |
in |
|
1433 |
Pretty.chunks ( |
|
1434 |
Pretty.block [ |
|
27103 | 1435 |
(str o suffix " ::" o deresolve_base) name, |
24219 | 1436 |
Pretty.brk 1, |
1437 |
pr_typscheme tyvars (vs, ty), |
|
1438 |
str ";" |
|
1439 |
] |
|
1440 |
:: map pr_eq eqs |
|
1441 |
) |
|
1442 |
end |
|
27103 | 1443 |
| pr_stmt (name, CodeThingol.Datatype (vs, [])) = |
24219 | 1444 |
let |
1445 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
1446 |
in |
|
1447 |
semicolon [ |
|
1448 |
str "data", |
|
27103 | 1449 |
pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
24219 | 1450 |
] |
1451 |
end |
|
27103 | 1452 |
| pr_stmt (name, CodeThingol.Datatype (vs, [(co, [ty])])) = |
24219 | 1453 |
let |
1454 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
1455 |
in |
|
1456 |
semicolon ( |
|
1457 |
str "newtype" |
|
27103 | 1458 |
:: pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
24219 | 1459 |
:: str "=" |
27103 | 1460 |
:: (str o deresolve_base) co |
24219 | 1461 |
:: pr_typ tyvars BR ty |
1462 |
:: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
|
1463 |
) |
|
1464 |
end |
|
27103 | 1465 |
| pr_stmt (name, CodeThingol.Datatype (vs, co :: cos)) = |
24219 | 1466 |
let |
1467 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
1468 |
fun pr_co (co, tys) = |
|
1469 |
concat ( |
|
27103 | 1470 |
(str o deresolve_base) co |
24219 | 1471 |
:: map (pr_typ tyvars BR) tys |
1472 |
) |
|
1473 |
in |
|
1474 |
semicolon ( |
|
1475 |
str "data" |
|
27103 | 1476 |
:: pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
24219 | 1477 |
:: str "=" |
1478 |
:: pr_co co |
|
1479 |
:: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos |
|
1480 |
@ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
|
1481 |
) |
|
1482 |
end |
|
27103 | 1483 |
| pr_stmt (name, CodeThingol.Class (v, (superclasses, classparams))) = |
24219 | 1484 |
let |
1485 |
val tyvars = CodeName.intro_vars [v] init_syms; |
|
24841 | 1486 |
fun pr_classparam (classparam, ty) = |
24219 | 1487 |
semicolon [ |
24841 | 1488 |
(str o classparam_name name) classparam, |
24219 | 1489 |
str "::", |
1490 |
pr_typ tyvars NOBR ty |
|
1491 |
] |
|
1492 |
in |
|
1493 |
Pretty.block_enclose ( |
|
1494 |
Pretty.block [ |
|
1495 |
str "class ", |
|
25621 | 1496 |
Pretty.block (pr_typcontext tyvars [(v, map fst superclasses)]), |
27103 | 1497 |
str (deresolve_base name ^ " " ^ CodeName.lookup_var tyvars v), |
24219 | 1498 |
str " where {" |
1499 |
], |
|
1500 |
str "};" |
|
24841 | 1501 |
) (map pr_classparam classparams) |
24219 | 1502 |
end |
27103 | 1503 |
| pr_stmt (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classparam_insts))) = |
24219 | 1504 |
let |
1505 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms; |
|
27304 | 1506 |
fun pr_instdef ((classparam, c_inst), thm) = |
24591 | 1507 |
semicolon [ |
24841 | 1508 |
(str o classparam_name class) classparam, |
24591 | 1509 |
str "=", |
27304 | 1510 |
pr_app tyvars thm false init_syms NOBR (c_inst, []) |
24591 | 1511 |
]; |
24219 | 1512 |
in |
1513 |
Pretty.block_enclose ( |
|
1514 |
Pretty.block [ |
|
1515 |
str "instance ", |
|
25621 | 1516 |
Pretty.block (pr_typcontext tyvars vs), |
24219 | 1517 |
str (class_name class ^ " "), |
1518 |
pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
|
1519 |
str " where {" |
|
1520 |
], |
|
1521 |
str "};" |
|
24841 | 1522 |
) (map pr_instdef classparam_insts) |
24219 | 1523 |
end; |
27103 | 1524 |
in pr_stmt end; |
24219 | 1525 |
|
24992 | 1526 |
fun pretty_haskell_monad c_bind = |
24219 | 1527 |
let |
27304 | 1528 |
fun pretty pr thm pat vars fxy [(t, _)] = |
24219 | 1529 |
let |
27304 | 1530 |
val pr_bind = pr_haskell_bind (K (K pr)) thm; |
24992 | 1531 |
fun pr_monad (NONE, t) vars = |
24219 | 1532 |
(semicolon [pr vars NOBR t], vars) |
24992 | 1533 |
| pr_monad (SOME (bind, true), t) vars = vars |
24219 | 1534 |
|> pr_bind NOBR bind |
1535 |
|>> (fn p => semicolon [p, str "<-", pr vars NOBR t]) |
|
24992 | 1536 |
| pr_monad (SOME (bind, false), t) vars = vars |
24219 | 1537 |
|> pr_bind NOBR bind |
1538 |
|>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]); |
|
24992 | 1539 |
val (binds, t) = implode_monad c_bind t; |
1540 |
val (ps, vars') = fold_map pr_monad binds vars; |
|
26010 | 1541 |
fun brack p = if fixity BR fxy then Pretty.block [str "(", p, str ")"] else p; |
24219 | 1542 |
in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end; |
1543 |
in (1, pretty) end; |
|
1544 |
||
27103 | 1545 |
fun haskell_program_of_program labelled_name module_name module_prefix reserved_names raw_module_alias program = |
24219 | 1546 |
let |
27103 | 1547 |
val module_alias = if is_some module_name then K module_name else raw_module_alias; |
1548 |
val reserved_names = Name.make_context reserved_names; |
|
1549 |
val mk_name_module = mk_name_module reserved_names module_prefix module_alias program; |
|
1550 |
fun add_stmt (name, (stmt, deps)) = |
|
24219 | 1551 |
let |
27103 | 1552 |
val (module_name, base) = dest_name name; |
1553 |
val module_name' = mk_name_module module_name; |
|
1554 |
val mk_name_stmt = yield_singleton Name.variants; |
|
24219 | 1555 |
fun add_fun upper (nsp_fun, nsp_typ) = |
1556 |
let |
|
24992 | 1557 |
val (base', nsp_fun') = |
27103 | 1558 |
mk_name_stmt (if upper then first_upper base else base) nsp_fun |
24219 | 1559 |
in (base', (nsp_fun', nsp_typ)) end; |
1560 |
fun add_typ (nsp_fun, nsp_typ) = |
|
1561 |
let |
|
27103 | 1562 |
val (base', nsp_typ') = mk_name_stmt (first_upper base) nsp_typ |
24219 | 1563 |
in (base', (nsp_fun, nsp_typ')) end; |
27103 | 1564 |
val add_name = case stmt |
1565 |
of CodeThingol.Fun _ => add_fun false |
|
1566 |
| CodeThingol.Datatype _ => add_typ |
|
1567 |
| CodeThingol.Datatypecons _ => add_fun true |
|
1568 |
| CodeThingol.Class _ => add_typ |
|
1569 |
| CodeThingol.Classrel _ => pair base |
|
1570 |
| CodeThingol.Classparam _ => add_fun false |
|
1571 |
| CodeThingol.Classinst _ => pair base; |
|
1572 |
fun add_stmt' base' = case stmt |
|
1573 |
of CodeThingol.Datatypecons _ => |
|
1574 |
cons (name, (NameSpace.append module_name' base', NONE)) |
|
1575 |
| CodeThingol.Classrel _ => I |
|
1576 |
| CodeThingol.Classparam _ => |
|
1577 |
cons (name, (NameSpace.append module_name' base', NONE)) |
|
1578 |
| _ => cons (name, (NameSpace.append module_name' base', SOME stmt)); |
|
24219 | 1579 |
in |
27103 | 1580 |
Symtab.map_default (module_name', ([], ([], (reserved_names, reserved_names)))) |
24219 | 1581 |
(apfst (fold (insert (op = : string * string -> bool)) deps)) |
27103 | 1582 |
#> `(fn program => add_name ((snd o snd o the o Symtab.lookup program) module_name')) |
24219 | 1583 |
#-> (fn (base', names) => |
27103 | 1584 |
(Symtab.map_entry module_name' o apsnd) (fn (stmts, _) => |
1585 |
(add_stmt' base' stmts, names))) |
|
24219 | 1586 |
end; |
27103 | 1587 |
val hs_program = fold add_stmt (AList.make (fn name => |
1588 |
(Graph.get_node program name, Graph.imm_succs program name)) |
|
1589 |
(Graph.strong_conn program |> flat)) Symtab.empty; |
|
1590 |
fun deresolver name = |
|
1591 |
(fst o the o AList.lookup (op =) ((fst o snd o the |
|
1592 |
o Symtab.lookup hs_program) ((mk_name_module o fst o dest_name) name))) name |
|
27024 | 1593 |
handle Option => error ("Unknown statement name: " ^ labelled_name name); |
27103 | 1594 |
in (deresolver, hs_program) end; |
1595 |
||
27304 | 1596 |
fun serialize_haskell module_prefix raw_module_name string_classes labelled_name |
27103 | 1597 |
reserved_names includes raw_module_alias |
1598 |
syntax_class syntax_tyco syntax_const program cs destination = |
|
1599 |
let |
|
27304 | 1600 |
val stmt_names = stmt_names_of_destination destination; |
1601 |
val module_name = if null stmt_names then raw_module_name else SOME "Code"; |
|
27103 | 1602 |
val (deresolver, hs_program) = haskell_program_of_program labelled_name |
1603 |
module_name module_prefix reserved_names raw_module_alias program; |
|
1604 |
val is_cons = CodeThingol.is_cons program; |
|
1605 |
val contr_classparam_typs = CodeThingol.contr_classparam_typs program; |
|
24219 | 1606 |
fun deriving_show tyco = |
1607 |
let |
|
1608 |
fun deriv _ "fun" = false |
|
1609 |
| deriv tycos tyco = member (op =) tycos tyco orelse |
|
27103 | 1610 |
case try (Graph.get_node program) tyco |
27024 | 1611 |
of SOME (CodeThingol.Datatype (_, cs)) => forall (deriv' (tyco :: tycos)) |
24219 | 1612 |
(maps snd cs) |
27024 | 1613 |
| NONE => true |
24219 | 1614 |
and deriv' tycos (tyco `%% tys) = deriv tycos tyco |
1615 |
andalso forall (deriv' tycos) tys |
|
1616 |
| deriv' _ (ITyVar _) = true |
|
1617 |
in deriv [] tyco end; |
|
27103 | 1618 |
val reserved_names = CodeName.make_vars reserved_names; |
1619 |
fun pr_stmt qualified = pr_haskell_stmt syntax_class syntax_tyco |
|
1620 |
syntax_const labelled_name reserved_names |
|
1621 |
(if qualified then deresolver else NameSpace.base o deresolver) |
|
1622 |
is_cons contr_classparam_typs |
|
24219 | 1623 |
(if string_classes then deriving_show else K false); |
27103 | 1624 |
fun pr_module name content = |
1625 |
(name, Pretty.chunks [ |
|
1626 |
str ("module " ^ name ^ " where {"), |
|
24992 | 1627 |
str "", |
1628 |
content, |
|
1629 |
str "", |
|
1630 |
str "}" |
|
27001 | 1631 |
]); |
27304 | 1632 |
fun serialize_module1 (module_name', (deps, (stmts, _))) = |
24219 | 1633 |
let |
27103 | 1634 |
val stmt_names = map fst stmts; |
1635 |
val deps' = subtract (op =) stmt_names deps |
|
24219 | 1636 |
|> distinct (op =) |
27103 | 1637 |
|> map_filter (try deresolver); |
1638 |
val qualified = is_none module_name andalso |
|
1639 |
map deresolver stmt_names @ deps' |
|
24219 | 1640 |
|> map NameSpace.base |
1641 |
|> has_duplicates (op =); |
|
27103 | 1642 |
val imports = deps' |
1643 |
|> map NameSpace.qualifier |
|
1644 |
|> distinct (op =); |
|
1645 |
fun pr_import_include (name, _) = str ("import " ^ name ^ ";"); |
|
1646 |
val pr_import_module = str o (if qualified |
|
24219 | 1647 |
then prefix "import qualified " |
1648 |
else prefix "import ") o suffix ";"; |
|
24992 | 1649 |
val content = Pretty.chunks ( |
27103 | 1650 |
map pr_import_include includes |
1651 |
@ map pr_import_module imports |
|
24992 | 1652 |
@ str "" |
1653 |
:: separate (str "") (map_filter |
|
27103 | 1654 |
(fn (name, (_, SOME stmt)) => SOME (pr_stmt qualified (name, stmt)) |
1655 |
| (_, (_, NONE)) => NONE) stmts) |
|
24992 | 1656 |
) |
27103 | 1657 |
in pr_module module_name' content end; |
27304 | 1658 |
fun serialize_module2 (_, (_, (stmts, _))) = Pretty.chunks ( |
1659 |
separate (str "") (map_filter |
|
1660 |
(fn (name, (_, SOME stmt)) => if null stmt_names |
|
1661 |
orelse member (op =) stmt_names name |
|
1662 |
then SOME (pr_stmt false (name, stmt)) |
|
1663 |
else NONE |
|
1664 |
| (_, (_, NONE)) => NONE) stmts)); |
|
1665 |
val serialize_module = case destination of String _ => pair "" o serialize_module2 |
|
1666 |
| _ => serialize_module1; |
|
27000 | 1667 |
fun write_module destination (modlname, content) = |
1668 |
let |
|
1669 |
val filename = case modlname |
|
1670 |
of "" => Path.explode "Main.hs" |
|
1671 |
| _ => (Path.ext "hs" o Path.explode o implode o separate "/" |
|
1672 |
o NameSpace.explode) modlname; |
|
1673 |
val pathname = Path.append destination filename; |
|
1674 |
val _ = File.mkdir (Path.dir pathname); |
|
27103 | 1675 |
in File.write pathname (code_of_pretty content) end |
1676 |
fun output Compile = error ("Haskell: no internal compilation") |
|
27304 | 1677 |
| output Export = K NONE o map (code_writeln o snd) |
27103 | 1678 |
| output (File destination) = K NONE o map (write_module destination) |
27436 | 1679 |
| output (String _) = SOME o rpair [] o cat_lines o map (code_of_pretty o snd); |
27103 | 1680 |
in |
1681 |
output destination (map (uncurry pr_module) includes |
|
1682 |
@ map serialize_module (Symtab.dest hs_program)) |
|
27000 | 1683 |
end; |
24219 | 1684 |
|
27000 | 1685 |
fun isar_seri_haskell module = |
1686 |
parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) |
|
1687 |
-- Scan.optional (Args.$$$ "string_classes" >> K true) false |
|
1688 |
>> (fn (module_prefix, string_classes) => |
|
27103 | 1689 |
serialize_haskell module_prefix module string_classes)); |
24219 | 1690 |
|
1691 |
||
1692 |
(** optional pretty serialization **) |
|
1693 |
||
1694 |
local |
|
1695 |
||
26448 | 1696 |
fun ocaml_char c = |
1697 |
let |
|
1698 |
fun chr i = |
|
1699 |
let |
|
1700 |
val xs = string_of_int i; |
|
1701 |
val ys = replicate_string (3 - length (explode xs)) "0"; |
|
1702 |
in "\\" ^ ys ^ xs end; |
|
1703 |
val i = ord c; |
|
1704 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
1705 |
then chr i else c |
|
1706 |
in s end; |
|
1707 |
||
1708 |
fun haskell_char c = |
|
1709 |
let |
|
1710 |
val s = ML_Syntax.print_char c; |
|
1711 |
in if s = "'" then "\\'" else s end; |
|
1712 |
||
24219 | 1713 |
val pretty : (string * { |
1714 |
pretty_char: string -> string, |
|
1715 |
pretty_string: string -> string, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1716 |
pretty_numeral: bool -> int -> string, |
24219 | 1717 |
pretty_list: Pretty.T list -> Pretty.T, |
1718 |
infix_cons: int * string |
|
1719 |
}) list = [ |
|
1720 |
("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char, |
|
26448 | 1721 |
pretty_string = quote o translate_string ML_Syntax.print_char, |
24219 | 1722 |
pretty_numeral = fn unbounded => fn k => |
24750 | 1723 |
if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)" |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1724 |
else string_of_int k, |
24219 | 1725 |
pretty_list = Pretty.enum "," "[" "]", |
1726 |
infix_cons = (7, "::")}), |
|
26448 | 1727 |
("OCaml", { pretty_char = enclose "'" "'" o ocaml_char, |
1728 |
pretty_string = quote o translate_string ocaml_char, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1729 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then |
24219 | 1730 |
if unbounded then |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1731 |
"(Big_int.big_int_of_int " ^ string_of_int k ^ ")" |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1732 |
else string_of_int k |
24219 | 1733 |
else |
1734 |
if unbounded then |
|
1735 |
"(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-" |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1736 |
o string_of_int o op ~) k ^ ")" |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1737 |
else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k, |
24219 | 1738 |
pretty_list = Pretty.enum ";" "[" "]", |
1739 |
infix_cons = (6, "::")}), |
|
26448 | 1740 |
("Haskell", { pretty_char = enclose "'" "'" o haskell_char, |
1741 |
pretty_string = quote o translate_string haskell_char, |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1742 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k |
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24621
diff
changeset
|
1743 |
else enclose "(" ")" (signed_string_of_int k), |
24219 | 1744 |
pretty_list = Pretty.enum "," "[" "]", |
1745 |
infix_cons = (5, ":")}) |
|
1746 |
]; |
|
1747 |
||
1748 |
in |
|
1749 |
||
1750 |
fun pr_pretty target = case AList.lookup (op =) pretty target |
|
1751 |
of SOME x => x |
|
1752 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
1753 |
||
1754 |
fun default_list (target_fxy, target_cons) pr fxy t1 t2 = |
|
1755 |
brackify_infix (target_fxy, R) fxy [ |
|
1756 |
pr (INFX (target_fxy, X)) t1, |
|
1757 |
str target_cons, |
|
1758 |
pr (INFX (target_fxy, R)) t2 |
|
1759 |
]; |
|
1760 |
||
1761 |
fun pretty_list c_nil c_cons target = |
|
1762 |
let |
|
1763 |
val pretty_ops = pr_pretty target; |
|
1764 |
val mk_list = #pretty_list pretty_ops; |
|
27304 | 1765 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 1766 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
1767 |
of SOME ts => mk_list (map (pr vars NOBR) ts) |
|
1768 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; |
|
1769 |
in (2, pretty) end; |
|
1770 |
||
1771 |
fun pretty_list_string c_nil c_cons c_char c_nibbles target = |
|
1772 |
let |
|
1773 |
val pretty_ops = pr_pretty target; |
|
1774 |
val mk_list = #pretty_list pretty_ops; |
|
1775 |
val mk_char = #pretty_char pretty_ops; |
|
1776 |
val mk_string = #pretty_string pretty_ops; |
|
27304 | 1777 |
fun pretty pr thm pat vars fxy [(t1, _), (t2, _)] = |
24219 | 1778 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
26448 | 1779 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
24219 | 1780 |
of SOME p => p |
26448 | 1781 |
| NONE => mk_list (map (pr vars NOBR) ts)) |
24219 | 1782 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; |
1783 |
in (2, pretty) end; |
|
1784 |
||
1785 |
fun pretty_char c_char c_nibbles target = |
|
1786 |
let |
|
1787 |
val mk_char = #pretty_char (pr_pretty target); |
|
27304 | 1788 |
fun pretty _ thm _ _ _ [(t1, _), (t2, _)] = |
24219 | 1789 |
case decode_char c_nibbles (t1, t2) |
1790 |
of SOME c => (str o mk_char) c |
|
27304 | 1791 |
| NONE => nerror thm "Illegal character expression"; |
24219 | 1792 |
in (2, pretty) end; |
1793 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
1794 |
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 target = |
24219 | 1795 |
let |
1796 |
val mk_numeral = #pretty_numeral (pr_pretty target); |
|
27304 | 1797 |
fun pretty _ thm _ _ _ [(t, _)] = |
1798 |
(str o mk_numeral unbounded o implode_numeral thm negative c_pls c_min c_bit0 c_bit1) t; |
|
24219 | 1799 |
in (1, pretty) end; |
1800 |
||
24992 | 1801 |
fun pretty_message c_char c_nibbles c_nil c_cons target = |
24219 | 1802 |
let |
1803 |
val pretty_ops = pr_pretty target; |
|
1804 |
val mk_char = #pretty_char pretty_ops; |
|
1805 |
val mk_string = #pretty_string pretty_ops; |
|
27304 | 1806 |
fun pretty _ thm _ _ _ [(t, _)] = |
24219 | 1807 |
case implode_list c_nil c_cons t |
1808 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts |
|
1809 |
of SOME p => p |
|
27304 | 1810 |
| NONE => nerror thm "Illegal message expression") |
1811 |
| NONE => nerror thm "Illegal message expression"; |
|
24219 | 1812 |
in (1, pretty) end; |
1813 |
||
1814 |
end; (*local*) |
|
1815 |
||
27000 | 1816 |
|
27304 | 1817 |
(** serializer use cases **) |
27000 | 1818 |
|
1819 |
(* evaluation *) |
|
1820 |
||
27103 | 1821 |
fun eval eval'' term_of reff thy ct args = |
27000 | 1822 |
let |
27103 | 1823 |
val _ = if null (term_frees (term_of ct)) then () else error ("Term " |
1824 |
^ quote (Syntax.string_of_term_global thy (term_of ct)) |
|
1825 |
^ " to be evaluated contains free variables"); |
|
1826 |
fun eval' program ((vs, ty), t) deps = |
|
1827 |
let |
|
1828 |
val _ = if CodeThingol.contains_dictvar t then |
|
1829 |
error "Term to be evaluated constains free dictionaries" else (); |
|
1830 |
val program' = program |
|
1831 |
|> Graph.new_node (CodeName.value_name, CodeThingol.Fun (([], ty), [(([], t), Drule.dummy_thm)])) |
|
1832 |
|> fold (curry Graph.add_edge CodeName.value_name) deps; |
|
27436 | 1833 |
val (value_code, [value_name']) = ml_code_of thy program' [CodeName.value_name]; |
1834 |
val sml_code = "let\n" ^ value_code ^ "\nin " ^ value_name' |
|
1835 |
^ space_implode " " (map (enclose "(" ")") args) ^ " end"; |
|
27103 | 1836 |
in ML_Context.evaluate Output.ml_output false reff sml_code end; |
1837 |
in eval'' thy (fn t => (t, eval')) ct end; |
|
1838 |
||
1839 |
fun eval_conv reff = eval CodeThingol.eval_conv Thm.term_of reff; |
|
1840 |
fun eval_term reff = eval CodeThingol.eval_term I reff; |
|
1841 |
||
1842 |
||
27304 | 1843 |
(* instrumentalization by antiquotation *) |
27103 | 1844 |
|
27436 | 1845 |
local |
1846 |
||
1847 |
structure CodeAntiqData = ProofDataFun |
|
1848 |
( |
|
27437 | 1849 |
type T = string list * (bool * (string * (string * (string * string) list) Susp.T)); |
1850 |
fun init _ = ([], (true, ("", Susp.value ("", [])))); |
|
27436 | 1851 |
); |
1852 |
||
1853 |
val is_first_occ = fst o snd o CodeAntiqData.get; |
|
1854 |
||
27437 | 1855 |
fun delayed_code thy consts () = |
1856 |
let |
|
1857 |
val (consts', program) = CodeThingol.consts_program thy consts; |
|
1858 |
val (ml_code, consts'') = ml_code_of thy program consts'; |
|
1859 |
val _ = if length consts <> length consts'' then |
|
1860 |
error ("One of the constants " ^ commas (map (quote o CodeUnit.string_of_const thy) consts) |
|
1861 |
^ "\nhas a user-defined serialization") else (); |
|
1862 |
in (ml_code, consts ~~ consts'') end; |
|
1863 |
||
27436 | 1864 |
fun register_const const ctxt = |
1865 |
let |
|
27437 | 1866 |
val (consts, (_, (struct_name, _))) = CodeAntiqData.get ctxt; |
1867 |
val consts' = insert (op =) const consts; |
|
27436 | 1868 |
val (struct_name', ctxt') = if struct_name = "" |
1869 |
then ML_Antiquote.variant "Code" ctxt |
|
1870 |
else (struct_name, ctxt); |
|
27437 | 1871 |
val acc_code = Susp.delay (delayed_code (ProofContext.theory_of ctxt) consts'); |
1872 |
in CodeAntiqData.put (consts', (false, (struct_name', acc_code))) ctxt' end; |
|
27436 | 1873 |
|
27437 | 1874 |
fun print_code struct_name is_first const ctxt = |
27304 | 1875 |
let |
27437 | 1876 |
val (consts, (_, (struct_code_name, acc_code))) = CodeAntiqData.get ctxt; |
1877 |
val (raw_ml_code, consts_map) = Susp.force acc_code; |
|
27436 | 1878 |
val const'' = NameSpace.append (NameSpace.append struct_name struct_code_name) |
27437 | 1879 |
((the o AList.lookup (op =) consts_map) const); |
27436 | 1880 |
val ml_code = if is_first then "\nstructure " ^ struct_code_name |
1881 |
^ " =\nstruct\n\n" ^ raw_ml_code ^ "\nend;\n\n" |
|
1882 |
else ""; |
|
1883 |
in (ml_code, const'') end; |
|
1884 |
||
1885 |
in |
|
1886 |
||
1887 |
fun ml_code_antiq raw_const {struct_name, background} = |
|
1888 |
let |
|
27437 | 1889 |
val const = CodeUnit.check_const (ProofContext.theory_of background) raw_const; |
27436 | 1890 |
val is_first = is_first_occ background; |
1891 |
val background' = register_const const background; |
|
27437 | 1892 |
in (print_code struct_name is_first const, background') end; |
27436 | 1893 |
|
1894 |
end; (*local*) |
|
27304 | 1895 |
|
1896 |
||
1897 |
(* code presentation *) |
|
1898 |
||
1899 |
fun code_of thy target module_name cs stmt_names = |
|
27103 | 1900 |
let |
1901 |
val (cs', program) = CodeThingol.consts_program thy cs; |
|
1902 |
in |
|
27304 | 1903 |
string stmt_names (serialize thy target (SOME module_name) [] program cs') |
27103 | 1904 |
end; |
27000 | 1905 |
|
1906 |
||
27304 | 1907 |
(* code generation *) |
1908 |
||
1909 |
fun read_const_exprs thy cs = |
|
1910 |
let |
|
1911 |
val (cs1, cs2) = CodeName.read_const_exprs thy cs; |
|
1912 |
val (cs3, program) = CodeThingol.consts_program thy cs2; |
|
1913 |
val cs4 = CodeThingol.transitivly_non_empty_funs program (abort_allowed thy); |
|
1914 |
val cs5 = map_filter |
|
1915 |
(fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3); |
|
1916 |
in fold (insert (op =)) cs5 cs1 end; |
|
1917 |
||
1918 |
fun cached_program thy = |
|
1919 |
let |
|
1920 |
val program = CodeThingol.cached_program thy; |
|
1921 |
in (CodeThingol.transitivly_non_empty_funs program (abort_allowed thy), program) end |
|
1922 |
||
1923 |
fun export_code thy cs seris = |
|
1924 |
let |
|
1925 |
val (cs', program) = if null cs then cached_program thy |
|
1926 |
else CodeThingol.consts_program thy cs; |
|
1927 |
fun mk_seri_dest dest = case dest |
|
1928 |
of NONE => compile |
|
1929 |
| SOME "-" => export |
|
1930 |
| SOME f => file (Path.explode f) |
|
1931 |
val _ = map (fn (((target, module), dest), args) => |
|
1932 |
(mk_seri_dest dest (serialize thy target module args program cs'))) seris; |
|
1933 |
in () end; |
|
1934 |
||
1935 |
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris; |
|
1936 |
||
1937 |
||
1938 |
(** serializer data **) |
|
1939 |
||
27000 | 1940 |
(* infix syntax *) |
1941 |
||
1942 |
datatype 'a mixfix = |
|
1943 |
Arg of fixity |
|
1944 |
| Pretty of Pretty.T; |
|
1945 |
||
1946 |
fun mk_mixfix prep_arg (fixity_this, mfx) = |
|
1947 |
let |
|
1948 |
fun is_arg (Arg _) = true |
|
1949 |
| is_arg _ = false; |
|
1950 |
val i = (length o filter is_arg) mfx; |
|
1951 |
fun fillin _ [] [] = |
|
1952 |
[] |
|
1953 |
| fillin pr (Arg fxy :: mfx) (a :: args) = |
|
1954 |
(pr fxy o prep_arg) a :: fillin pr mfx args |
|
1955 |
| fillin pr (Pretty p :: mfx) args = |
|
27304 | 1956 |
p :: fillin pr mfx args; |
27000 | 1957 |
in |
1958 |
(i, fn pr => fn fixity_ctxt => fn args => |
|
1959 |
gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args)) |
|
1960 |
end; |
|
1961 |
||
1962 |
fun parse_infix prep_arg (x, i) s = |
|
1963 |
let |
|
1964 |
val l = case x of L => INFX (i, L) | _ => INFX (i, X); |
|
1965 |
val r = case x of R => INFX (i, R) | _ => INFX (i, X); |
|
1966 |
in |
|
1967 |
mk_mixfix prep_arg (INFX (i, x), |
|
1968 |
[Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r]) |
|
1969 |
end; |
|
1970 |
||
1971 |
||
1972 |
(* data access *) |
|
24219 | 1973 |
|
1974 |
local |
|
1975 |
||
24992 | 1976 |
fun cert_class thy class = |
1977 |
let |
|
1978 |
val _ = AxClass.get_info thy class; |
|
1979 |
in class end; |
|
1980 |
||
27103 | 1981 |
fun read_class thy = cert_class thy o Sign.intern_class thy; |
24992 | 1982 |
|
1983 |
fun cert_tyco thy tyco = |
|
1984 |
let |
|
1985 |
val _ = if Sign.declared_tyname thy tyco then () |
|
1986 |
else error ("No such type constructor: " ^ quote tyco); |
|
1987 |
in tyco end; |
|
1988 |
||
27103 | 1989 |
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy; |
24219 | 1990 |
|
1991 |
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy = |
|
1992 |
let |
|
24841 | 1993 |
val class = prep_class thy raw_class; |
1994 |
val class' = CodeName.class thy class; |
|
1995 |
fun mk_classparam c = case AxClass.class_of_param thy c |
|
1996 |
of SOME class'' => if class = class'' then CodeName.const thy c |
|
24219 | 1997 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) |
1998 |
| NONE => error ("Not a class operation: " ^ quote c); |
|
24841 | 1999 |
fun mk_syntax_params raw_params = AList.lookup (op =) |
2000 |
((map o apfst) (mk_classparam o prep_const thy) raw_params); |
|
24219 | 2001 |
in case raw_syn |
24841 | 2002 |
of SOME (syntax, raw_params) => |
24219 | 2003 |
thy |
27024 | 2004 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 2005 |
(Symtab.update (class', (syntax, mk_syntax_params raw_params))) |
24219 | 2006 |
| NONE => |
2007 |
thy |
|
27024 | 2008 |
|> (map_name_syntax target o apfst o apfst) |
24841 | 2009 |
(Symtab.delete_safe class') |
24219 | 2010 |
end; |
2011 |
||
2012 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = |
|
2013 |
let |
|
2014 |
val inst = CodeName.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); |
|
2015 |
in if add_del then |
|
2016 |
thy |
|
27024 | 2017 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 2018 |
(Symtab.update (inst, ())) |
2019 |
else |
|
2020 |
thy |
|
27024 | 2021 |
|> (map_name_syntax target o apfst o apsnd) |
24219 | 2022 |
(Symtab.delete_safe inst) |
2023 |
end; |
|
2024 |
||
2025 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = |
|
2026 |
let |
|
2027 |
val tyco = prep_tyco thy raw_tyco; |
|
2028 |
val tyco' = if tyco = "fun" then "fun" else CodeName.tyco thy tyco; |
|
2029 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco |
|
2030 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) |
|
2031 |
else syntax |
|
2032 |
in case raw_syn |
|
2033 |
of SOME syntax => |
|
2034 |
thy |
|
27024 | 2035 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 2036 |
(Symtab.update (tyco', check_args syntax)) |
2037 |
| NONE => |
|
2038 |
thy |
|
27024 | 2039 |
|> (map_name_syntax target o apsnd o apfst) |
24219 | 2040 |
(Symtab.delete_safe tyco') |
2041 |
end; |
|
2042 |
||
27304 | 2043 |
fun simple_const_syntax x = (Option.map o apsnd) |
2044 |
(fn pretty => fn pr => fn thm => fn pat => fn vars => pretty (pr vars)) x; |
|
2045 |
||
24219 | 2046 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy = |
2047 |
let |
|
2048 |
val c = prep_const thy raw_c; |
|
2049 |
val c' = CodeName.const thy c; |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2050 |
fun check_args (syntax as (n, _)) = if n > CodeUnit.no_args thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2051 |
then error ("Too many arguments in syntax for constant " ^ quote c) |
24219 | 2052 |
else syntax; |
2053 |
in case raw_syn |
|
2054 |
of SOME syntax => |
|
2055 |
thy |
|
27024 | 2056 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 2057 |
(Symtab.update (c', check_args syntax)) |
2058 |
| NONE => |
|
2059 |
thy |
|
27024 | 2060 |
|> (map_name_syntax target o apsnd o apsnd) |
24219 | 2061 |
(Symtab.delete_safe c') |
2062 |
end; |
|
2063 |
||
2064 |
fun add_reserved target = |
|
2065 |
let |
|
2066 |
fun add sym syms = if member (op =) syms sym |
|
2067 |
then error ("Reserved symbol " ^ quote sym ^ " already declared") |
|
2068 |
else insert (op =) sym syms |
|
27103 | 2069 |
in map_reserved target o add end; |
24992 | 2070 |
|
2071 |
fun add_include target = |
|
2072 |
let |
|
2073 |
fun add (name, SOME content) incls = |
|
2074 |
let |
|
2075 |
val _ = if Symtab.defined incls name |
|
2076 |
then warning ("Overwriting existing include " ^ name) |
|
2077 |
else (); |
|
2078 |
in Symtab.update (name, str content) incls end |
|
2079 |
| add (name, NONE) incls = |
|
2080 |
Symtab.delete name incls; |
|
27103 | 2081 |
in map_includes target o add end; |
24219 | 2082 |
|
27103 | 2083 |
fun add_module_alias target = |
24992 | 2084 |
map_module_alias target o Symtab.update o apsnd CodeName.check_modulename; |
24219 | 2085 |
|
27710 | 2086 |
fun add_monad target raw_c_run raw_c_bind thy = |
24992 | 2087 |
let |
27103 | 2088 |
val c_run = CodeUnit.read_const thy raw_c_run; |
2089 |
val c_bind = CodeUnit.read_const thy raw_c_bind; |
|
2090 |
val c_bind' = CodeName.const thy c_bind; |
|
24992 | 2091 |
in if target = target_Haskell then |
2092 |
thy |
|
27103 | 2093 |
|> gen_add_syntax_const (K I) target_Haskell c_run |
2094 |
(SOME (pretty_haskell_monad c_bind')) |
|
2095 |
|> gen_add_syntax_const (K I) target_Haskell c_bind |
|
27304 | 2096 |
(simple_const_syntax (SOME (parse_infix fst (L, 1) ">>="))) |
27710 | 2097 |
else error "Only Haskell target allows for monad syntax" end; |
24219 | 2098 |
|
27103 | 2099 |
fun gen_allow_abort prep_cs raw_c thy = |
24841 | 2100 |
let |
2101 |
val c = prep_cs thy raw_c; |
|
2102 |
val c' = CodeName.const thy c; |
|
2103 |
in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end; |
|
2104 |
||
24219 | 2105 |
fun zip_list (x::xs) f g = |
2106 |
f |
|
2107 |
#-> (fn y => |
|
2108 |
fold_map (fn x => g |-- f >> pair x) xs |
|
2109 |
#-> (fn xys => pair ((x, y) :: xys))); |
|
2110 |
||
27000 | 2111 |
|
27103 | 2112 |
(* concrete syntax *) |
27000 | 2113 |
|
24219 | 2114 |
structure P = OuterParse |
2115 |
and K = OuterKeyword |
|
2116 |
||
2117 |
fun parse_multi_syntax parse_thing parse_syntax = |
|
2118 |
P.and_list1 parse_thing |
|
2119 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- |
|
2120 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); |
|
2121 |
||
2122 |
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr"); |
|
2123 |
||
27000 | 2124 |
fun parse_mixfix prep_arg s = |
2125 |
let |
|
2126 |
val sym_any = Scan.one Symbol.is_regular; |
|
2127 |
val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat ( |
|
2128 |
($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR)) |
|
2129 |
|| ($$ "_" >> K (Arg BR)) |
|
2130 |
|| ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)) |
|
2131 |
|| (Scan.repeat1 |
|
2132 |
( $$ "'" |-- sym_any |
|
2133 |
|| Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")") |
|
2134 |
sym_any) >> (Pretty o str o implode))); |
|
2135 |
in case Scan.finite Symbol.stopper parse (Symbol.explode s) |
|
2136 |
of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p) |
|
2137 |
| ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p) |
|
2138 |
| _ => Scan.!! |
|
2139 |
(the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail () |
|
2140 |
end; |
|
2141 |
||
24219 | 2142 |
fun parse_syntax prep_arg xs = |
2143 |
Scan.option (( |
|
2144 |
((P.$$$ infixK >> K X) |
|
2145 |
|| (P.$$$ infixlK >> K L) |
|
2146 |
|| (P.$$$ infixrK >> K R)) |
|
2147 |
-- P.nat >> parse_infix prep_arg |
|
2148 |
|| Scan.succeed (parse_mixfix prep_arg)) |
|
2149 |
-- P.string |
|
2150 |
>> (fn (parse, s) => parse s)) xs; |
|
2151 |
||
2152 |
in |
|
2153 |
||
2154 |
val parse_syntax = parse_syntax; |
|
2155 |
||
2156 |
val add_syntax_class = gen_add_syntax_class cert_class (K I); |
|
2157 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; |
|
2158 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; |
|
2159 |
val add_syntax_const = gen_add_syntax_const (K I); |
|
27103 | 2160 |
val allow_abort = gen_allow_abort (K I); |
24219 | 2161 |
|
2162 |
val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const; |
|
2163 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; |
|
2164 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; |
|
2165 |
val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const; |
|
27103 | 2166 |
val allow_abort_cmd = gen_allow_abort CodeUnit.read_const; |
24219 | 2167 |
|
2168 |
fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco; |
|
27304 | 2169 |
fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o simple_const_syntax); |
24219 | 2170 |
|
2171 |
fun add_undefined target undef target_undefined thy = |
|
2172 |
let |
|
27304 | 2173 |
fun pr _ _ _ _ _ _ = str target_undefined; |
24219 | 2174 |
in |
2175 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2176 |
|> add_syntax_const target undef (SOME (~1, pr)) |
24219 | 2177 |
end; |
2178 |
||
2179 |
fun add_pretty_list target nill cons thy = |
|
2180 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2181 |
val nil' = CodeName.const thy nill; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2182 |
val cons' = CodeName.const thy cons; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2183 |
val pr = pretty_list nil' cons' target; |
24219 | 2184 |
in |
2185 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2186 |
|> add_syntax_const target cons (SOME pr) |
24219 | 2187 |
end; |
2188 |
||
2189 |
fun add_pretty_list_string target nill cons charr nibbles thy = |
|
2190 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2191 |
val nil' = CodeName.const thy nill; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2192 |
val cons' = CodeName.const thy cons; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2193 |
val charr' = CodeName.const thy charr; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2194 |
val nibbles' = map (CodeName.const thy) nibbles; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2195 |
val pr = pretty_list_string nil' cons' charr' nibbles' target; |
24219 | 2196 |
in |
2197 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2198 |
|> add_syntax_const target cons (SOME pr) |
24219 | 2199 |
end; |
2200 |
||
2201 |
fun add_pretty_char target charr nibbles thy = |
|
2202 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2203 |
val charr' = CodeName.const thy charr; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2204 |
val nibbles' = map (CodeName.const thy) nibbles; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2205 |
val pr = pretty_char charr' nibbles' target; |
24219 | 2206 |
in |
2207 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2208 |
|> add_syntax_const target charr (SOME pr) |
24219 | 2209 |
end; |
2210 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
2211 |
fun add_pretty_numeral target unbounded negative number_of pls min bit0 bit1 thy = |
24219 | 2212 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2213 |
val pls' = CodeName.const thy pls; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2214 |
val min' = CodeName.const thy min; |
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
2215 |
val bit0' = CodeName.const thy bit0; |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
2216 |
val bit1' = CodeName.const thy bit1; |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26010
diff
changeset
|
2217 |
val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' target; |
24219 | 2218 |
in |
2219 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2220 |
|> add_syntax_const target number_of (SOME pr) |
24219 | 2221 |
end; |
2222 |
||
24992 | 2223 |
fun add_pretty_message target charr nibbles nill cons str thy = |
24219 | 2224 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2225 |
val charr' = CodeName.const thy charr; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2226 |
val nibbles' = map (CodeName.const thy) nibbles; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2227 |
val nil' = CodeName.const thy nill; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2228 |
val cons' = CodeName.const thy cons; |
24992 | 2229 |
val pr = pretty_message charr' nibbles' nil' cons' target; |
24219 | 2230 |
in |
2231 |
thy |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24381
diff
changeset
|
2232 |
|> add_syntax_const target str (SOME pr) |
24219 | 2233 |
end; |
2234 |
||
24867 | 2235 |
|
27103 | 2236 |
|
27304 | 2237 |
(** Isar setup **) |
27103 | 2238 |
|
2239 |
val (inK, module_nameK, fileK) = ("in", "module_name", "file"); |
|
2240 |
||
2241 |
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
|
2242 |
(Scan.repeat P.term_group |
27103 | 2243 |
-- Scan.repeat (P.$$$ inK |-- P.name |
2244 |
-- Scan.option (P.$$$ module_nameK |-- P.name) |
|
2245 |
-- Scan.option (P.$$$ fileK |-- P.name) |
|
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27757
diff
changeset
|
2246 |
-- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") [] |
27103 | 2247 |
) >> (fn (raw_cs, seris) => cmd raw_cs seris)); |
2248 |
||
27353
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
27346
diff
changeset
|
2249 |
val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK, inK, module_nameK, fileK]; |
24867 | 2250 |
|
2251 |
val _ = |
|
24992 | 2252 |
OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl ( |
24219 | 2253 |
parse_multi_syntax P.xname |
2254 |
(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
|
2255 |
(P.term_group --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) [])) |
24219 | 2256 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
2257 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) |
|
2258 |
); |
|
2259 |
||
24867 | 2260 |
val _ = |
24992 | 2261 |
OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl ( |
24219 | 2262 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
2263 |
((P.minus >> K true) || Scan.succeed false) |
|
2264 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
2265 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) |
|
2266 |
); |
|
2267 |
||
24867 | 2268 |
val _ = |
24992 | 2269 |
OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl ( |
24219 | 2270 |
parse_multi_syntax P.xname (parse_syntax I) |
2271 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
2272 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) |
|
2273 |
); |
|
2274 |
||
24867 | 2275 |
val _ = |
24992 | 2276 |
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
|
2277 |
parse_multi_syntax P.term_group (parse_syntax fst) |
24219 | 2278 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
27304 | 2279 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (simple_const_syntax syn)) syns) |
24219 | 2280 |
); |
2281 |
||
24867 | 2282 |
val _ = |
24992 | 2283 |
OuterSyntax.command "code_monad" "define code syntax for monads" 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
|
2284 |
P.term_group -- P.term_group -- P.name |
27710 | 2285 |
>> (fn ((raw_run, raw_bind), target) => Toplevel.theory |
2286 |
(add_monad target raw_run raw_bind)) |
|
24219 | 2287 |
); |
2288 |
||
24867 | 2289 |
val _ = |
24992 | 2290 |
OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl ( |
24219 | 2291 |
P.name -- Scan.repeat1 P.name |
2292 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) |
|
24841 | 2293 |
); |
24219 | 2294 |
|
24867 | 2295 |
val _ = |
24992 | 2296 |
OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl ( |
2297 |
P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s)) |
|
2298 |
>> (fn ((target, name), content) => (Toplevel.theory o add_include target) |
|
2299 |
(name, content)) |
|
2300 |
); |
|
2301 |
||
2302 |
val _ = |
|
2303 |
OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl ( |
|
24219 | 2304 |
P.name -- Scan.repeat1 (P.name -- P.name) |
27103 | 2305 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames) |
2306 |
); |
|
2307 |
||
2308 |
val _ = |
|
2309 |
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
|
2310 |
Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd) |
24841 | 2311 |
); |
24219 | 2312 |
|
24867 | 2313 |
val _ = |
27103 | 2314 |
OuterSyntax.command "export_code" "generate executable code for constants" |
2315 |
K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of))); |
|
2316 |
||
2317 |
fun shell_command thyname cmd = Toplevel.program (fn _ => |
|
27845 | 2318 |
(use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd)) |
2319 |
((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd) |
|
27103 | 2320 |
of SOME f => (writeln "Now generating code..."; f (theory thyname)) |
2321 |
| NONE => error ("Bad directive " ^ quote cmd))) |
|
2322 |
handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure; |
|
2323 |
||
27868 | 2324 |
val _ = ML_Context.add_antiq "code" (fn _ => Args.term >> ml_code_antiq); |
24219 | 2325 |
|
2326 |
||
27000 | 2327 |
(* serializer setup, including serializer defaults *) |
2328 |
||
24219 | 2329 |
val setup = |
27103 | 2330 |
add_target (target_SML, isar_seri_sml) |
2331 |
#> add_target (target_OCaml, isar_seri_ocaml) |
|
2332 |
#> add_target (target_Haskell, isar_seri_haskell) |
|
24219 | 2333 |
#> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
27024 | 2334 |
brackify_infix (1, R) fxy [ |
24219 | 2335 |
pr_typ (INFX (1, X)) ty1, |
2336 |
str "->", |
|
2337 |
pr_typ (INFX (1, R)) ty2 |
|
2338 |
])) |
|
2339 |
#> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
|
27024 | 2340 |
brackify_infix (1, R) fxy [ |
24219 | 2341 |
pr_typ (INFX (1, X)) ty1, |
2342 |
str "->", |
|
2343 |
pr_typ (INFX (1, R)) ty2 |
|
2344 |
])) |
|
2345 |
#> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
|
2346 |
brackify_infix (1, R) fxy [ |
|
2347 |
pr_typ (INFX (1, X)) ty1, |
|
2348 |
str "->", |
|
2349 |
pr_typ (INFX (1, R)) ty2 |
|
2350 |
])) |
|
2351 |
#> fold (add_reserved "SML") ML_Syntax.reserved_names |
|
2352 |
#> fold (add_reserved "SML") |
|
2353 |
["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)] |
|
2354 |
#> fold (add_reserved "OCaml") [ |
|
2355 |
"and", "as", "assert", "begin", "class", |
|
2356 |
"constraint", "do", "done", "downto", "else", "end", "exception", |
|
2357 |
"external", "false", "for", "fun", "function", "functor", "if", |
|
2358 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method", |
|
2359 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec", |
|
2360 |
"sig", "struct", "then", "to", "true", "try", "type", "val", |
|
2361 |
"virtual", "when", "while", "with" |
|
2362 |
] |
|
2363 |
#> fold (add_reserved "OCaml") ["failwith", "mod"] |
|
2364 |
#> fold (add_reserved "Haskell") [ |
|
2365 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
|
2366 |
"import", "default", "forall", "let", "in", "class", "qualified", "data", |
|
2367 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module" |
|
2368 |
] |
|
2369 |
#> fold (add_reserved "Haskell") [ |
|
2370 |
"Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int", |
|
2371 |
"Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded", |
|
2372 |
"Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor", |
|
2373 |
"AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException", |
|
2374 |
"BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException", |
|
2375 |
"Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure", |
|
2376 |
"ExitSuccess", "False", "GT", "HeapOverflow", |
|
2377 |
"IOError", "IOException", "IllegalOperation", |
|
2378 |
"IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError", |
|
2379 |
"NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow", |
|
2380 |
"PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError", |
|
2381 |
"RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow", |
|
2382 |
"ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow", |
|
2383 |
"UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all", |
|
2384 |
"and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan", |
|
2385 |
"atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen", |
|
2386 |
"boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break", |
|
2387 |
"catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const", |
|
2388 |
"cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod", |
|
2389 |
"doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem", |
|
2390 |
"emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo", |
|
2391 |
"enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip", |
|
2392 |
"floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational", |
|
2393 |
"floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble", |
|
2394 |
"fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj", |
|
2395 |
"fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head", |
|
2396 |
"id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha", |
|
2397 |
"isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite", |
|
2398 |
"isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'", |
|
2399 |
"last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log", |
|
2400 |
"logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum", |
|
2401 |
"maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem", |
|
2402 |
"null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo", |
|
2403 |
"numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", |
|
2404 |
"print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn", |
|
2405 |
"quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat", |
|
2406 |
"rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile", |
|
2407 |
"readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn", |
|
2408 |
"readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational", |
|
2409 |
"realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse", |
|
2410 |
"round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence", |
|
2411 |
"sequence_", "show", "showChar", "showException", "showField", "showList", |
|
2412 |
"showLitChar", "showParen", "showString", "shows", "showsPrec", "significand", |
|
2413 |
"signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract", |
|
2414 |
"succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult", |
|
2415 |
"throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry", |
|
2416 |
"undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords", |
|
2417 |
"unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3" |
|
2418 |
] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*); |
|
2419 |
||
2420 |
end; (*local*) |
|
2421 |
||
2422 |
end; (*struct*) |