28 val tyco_has_serialization: theory -> string list -> string -> bool; |
28 val tyco_has_serialization: theory -> string list -> string -> bool; |
29 |
29 |
30 val eval_verbose: bool ref; |
30 val eval_verbose: bool ref; |
31 val eval_term: theory -> CodegenThingol.code |
31 val eval_term: theory -> CodegenThingol.code |
32 -> (string * 'a option ref) * CodegenThingol.iterm -> 'a; |
32 -> (string * 'a option ref) * CodegenThingol.iterm -> 'a; |
33 val sml_code_width: int ref; |
33 val code_width: int ref; |
34 end; |
34 end; |
35 |
35 |
36 structure CodegenSerializer: CODEGEN_SERIALIZER = |
36 structure CodegenSerializer: CODEGEN_SERIALIZER = |
37 struct |
37 struct |
38 |
38 |
39 open BasicCodegenThingol; |
39 open BasicCodegenThingol; |
40 val tracing = CodegenThingol.tracing; |
40 val tracing = CodegenThingol.tracing; |
41 |
41 |
42 (** syntax **) |
42 (** basics **) |
43 |
|
44 (* basics *) |
|
45 |
43 |
46 infixr 5 @@; |
44 infixr 5 @@; |
47 infixr 5 @|; |
45 infixr 5 @|; |
48 fun x @@ y = [x, y]; |
46 fun x @@ y = [x, y]; |
49 fun xs @| y = xs @ [y]; |
47 fun xs @| y = xs @ [y]; |
|
48 val str = setmp print_mode [] Pretty.str; |
|
49 val concat = Pretty.block o Pretty.breaks; |
|
50 fun semicolon ps = Pretty.block [concat ps, str ";"]; |
|
51 |
|
52 (** syntax **) |
50 |
53 |
51 datatype lrx = L | R | X; |
54 datatype lrx = L | R | X; |
52 |
55 |
53 datatype fixity = |
56 datatype fixity = |
54 BR |
57 BR |
55 | NOBR |
58 | NOBR |
56 | INFX of (int * lrx); |
59 | INFX of (int * lrx); |
|
60 |
|
61 val APP = INFX (~1, L); |
57 |
62 |
58 type 'a pretty_syntax = int * (fixity -> (fixity -> 'a -> Pretty.T) |
63 type 'a pretty_syntax = int * (fixity -> (fixity -> 'a -> Pretty.T) |
59 -> 'a list -> Pretty.T); |
64 -> 'a list -> Pretty.T); |
60 |
65 |
61 fun eval_lrx L L = false |
66 fun eval_lrx L L = false |
333 val (ps, t') = CodegenThingol.unfold_abs t; |
338 val (ps, t') = CodegenThingol.unfold_abs t; |
334 fun pr ((v, NONE), _) vars = |
339 fun pr ((v, NONE), _) vars = |
335 let |
340 let |
336 val vars' = CodegenThingol.intro_vars [v] vars; |
341 val vars' = CodegenThingol.intro_vars [v] vars; |
337 in |
342 in |
338 ((Pretty.block o Pretty.breaks) [str "fn", str (CodegenThingol.lookup_var vars' v), str "=>"], vars') |
343 (concat [str "fn", str (CodegenThingol.lookup_var vars' v), str "=>"], vars') |
339 end |
344 end |
340 | pr ((v, SOME p), _) vars = |
345 | pr ((v, SOME p), _) vars = |
341 let |
346 let |
342 val vars' = CodegenThingol.intro_vars [v] vars; |
347 val vars' = CodegenThingol.intro_vars [v] vars; |
343 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
348 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
344 val vars'' = CodegenThingol.intro_vars vs vars'; |
349 val vars'' = CodegenThingol.intro_vars vs vars'; |
345 in |
350 in |
346 ((Pretty.block o Pretty.breaks) [str "fn", str (CodegenThingol.lookup_var vars' v), str "as", |
351 (concat [str "fn", str (CodegenThingol.lookup_var vars' v), str "as", |
347 pr_term vars'' NOBR p, str "=>"], vars'') |
352 pr_term vars'' NOBR p, str "=>"], vars'') |
348 end; |
353 end; |
349 val (ps', vars') = fold_map pr ps vars; |
354 val (ps', vars') = fold_map pr ps vars; |
350 in brackify BR (ps' @ [pr_term vars' NOBR t']) end |
355 in brackify BR (ps' @ [pr_term vars' NOBR t']) end |
351 | pr_term vars fxy (INum n) = |
356 | pr_term vars fxy (INum n) = |
484 in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
489 in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
485 | pr_def (MLClass (class, (superclasses, (v, classops)))) = |
490 | pr_def (MLClass (class, (superclasses, (v, classops)))) = |
486 let |
491 let |
487 val w = dictvar v; |
492 val w = dictvar v; |
488 fun pr_superclass class = |
493 fun pr_superclass class = |
489 (Pretty.block o Pretty.breaks o map str) [ |
494 (concat o map str) [ |
490 label class, ":", "'" ^ v, deresolv class |
495 label class, ":", "'" ^ v, deresolv class |
491 ]; |
496 ]; |
492 fun pr_classop (classop, ty) = |
497 fun pr_classop (classop, ty) = |
493 (Pretty.block o Pretty.breaks) [ |
498 concat [ |
494 (*FIXME?*) |
499 (*FIXME?*) |
495 (str o mk_classop_name) classop, str ":", pr_typ NOBR ty |
500 (str o mk_classop_name) classop, str ":", pr_typ NOBR ty |
496 ]; |
501 ]; |
497 fun pr_classop_fun (classop, _) = |
502 fun pr_classop_fun (classop, _) = |
498 (Pretty.block o Pretty.breaks) [ |
503 concat [ |
499 str "fun", |
504 str "fun", |
500 (str o deresolv) classop, |
505 (str o deresolv) classop, |
501 Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
506 Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
502 str "=", |
507 str "=", |
503 str ("#" ^ mk_classop_name classop), |
508 str ("#" ^ mk_classop_name classop), |
504 str (w ^ ";") |
509 str (w ^ ";") |
505 ]; |
510 ]; |
506 in |
511 in |
507 Pretty.chunks ( |
512 Pretty.chunks ( |
508 (Pretty.block o Pretty.breaks) [ |
513 concat [ |
509 str ("type '" ^ v), |
514 str ("type '" ^ v), |
510 (str o deresolv) class, |
515 (str o deresolv) class, |
511 str "=", |
516 str "=", |
512 Pretty.enum "," "{" "};" ( |
517 Pretty.enum "," "{" "};" ( |
513 map pr_superclass superclasses @ map pr_classop classops |
518 map pr_superclass superclasses @ map pr_classop classops |
842 in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
847 in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
843 | pr_def (MLClass (class, (superclasses, (v, classops)))) = |
848 | pr_def (MLClass (class, (superclasses, (v, classops)))) = |
844 let |
849 let |
845 val w = dictvar v; |
850 val w = dictvar v; |
846 fun pr_superclass class = |
851 fun pr_superclass class = |
847 (Pretty.block o Pretty.breaks o map str) [ |
852 (concat o map str) [ |
848 deresolv class, ":", "'" ^ v, deresolv class |
853 deresolv class, ":", "'" ^ v, deresolv class |
849 ]; |
854 ]; |
850 fun pr_classop (classop, ty) = |
855 fun pr_classop (classop, ty) = |
851 (Pretty.block o Pretty.breaks) [ |
856 concat [ |
852 (str o deresolv) classop, str ":", pr_typ NOBR ty |
857 (str o deresolv) classop, str ":", pr_typ NOBR ty |
853 ]; |
858 ]; |
854 fun pr_classop_fun (classop, _) = |
859 fun pr_classop_fun (classop, _) = |
855 (Pretty.block o Pretty.breaks) [ |
860 concat [ |
856 str "let", |
861 str "let", |
857 (str o deresolv) classop, |
862 (str o deresolv) classop, |
858 Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
863 Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
859 str "=", |
864 str "=", |
860 str (w ^ "." ^ deresolv classop ^ ";;") |
865 str (w ^ "." ^ deresolv classop ^ ";;") |
861 ]; |
866 ]; |
862 in |
867 in |
863 Pretty.chunks ( |
868 Pretty.chunks ( |
864 (Pretty.block o Pretty.breaks) [ |
869 concat [ |
865 str ("type '" ^ v), |
870 str ("type '" ^ v), |
866 (str o deresolv) class, |
871 (str o deresolv) class, |
867 str "=", |
872 str "=", |
868 Pretty.enum ";" "{" "};;" ( |
873 Pretty.enum ";" "{" "};;" ( |
869 map pr_superclass superclasses @ map pr_classop classops |
874 map pr_superclass superclasses @ map pr_classop classops |
1082 (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)) |
1088 (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)) |
1083 in output p end; |
1089 in output p end; |
1084 |
1090 |
1085 val isar_seri_sml = |
1091 val isar_seri_sml = |
1086 let |
1092 let |
1087 fun output_file file p = File.write (Path.explode file) (Pretty.output p ^ "\n"); |
1093 fun output_file file = File.write (Path.explode file) o code_output; |
1088 fun output_diag p = Pretty.writeln p; |
1094 val output_diag = writeln o code_output; |
1089 fun output_internal p = use_text Output.ml_output false (Pretty.output p); |
1095 val output_internal = use_text Output.ml_output false o code_output; |
1090 in |
1096 in |
1091 parse_args ((Args.$$$ "-" >> K output_diag |
1097 parse_args ((Args.$$$ "-" >> K output_diag |
1092 || Args.$$$ "#" >> K output_internal |
1098 || Args.$$$ "#" >> K output_internal |
1093 || Args.name >> output_file) |
1099 || Args.name >> output_file) |
1094 >> (fn output => seri_ml pr_sml pr_sml_modl output)) |
1100 >> (fn output => seri_ml pr_sml pr_sml_modl output)) |
1095 end; |
1101 end; |
1096 |
1102 |
1097 val isar_seri_ocaml = |
1103 val isar_seri_ocaml = |
1098 let |
1104 let |
1099 fun output_file file p = File.write (Path.explode file) (Pretty.output p ^ "\n"); |
1105 fun output_file file = File.write (Path.explode file) o code_output; |
1100 fun output_diag p = Pretty.writeln p; |
1106 val output_diag = writeln o code_output; |
1101 in |
1107 in |
1102 parse_args ((Args.$$$ "-" >> K output_diag |
1108 parse_args ((Args.$$$ "-" >> K output_diag |
1103 || Args.name >> output_file) |
1109 || Args.name >> output_file) |
1104 >> (fn output => seri_ml pr_ocaml pr_ocaml_modl output)) |
1110 >> (fn output => seri_ml pr_ocaml pr_ocaml_modl output)) |
1105 end; |
1111 end; |
1200 fun pr ((p, _), t) vars = |
1206 fun pr ((p, _), t) vars = |
1201 let |
1207 let |
1202 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
1208 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
1203 val vars' = CodegenThingol.intro_vars vs vars; |
1209 val vars' = CodegenThingol.intro_vars vs vars; |
1204 in |
1210 in |
1205 ((Pretty.block o Pretty.breaks) [ |
1211 (semicolon [ |
1206 pr_term vars' BR p, |
1212 pr_term vars' BR p, |
1207 str "=", |
1213 str "=", |
1208 pr_term vars NOBR t |
1214 pr_term vars NOBR t |
1209 ], vars') |
1215 ], vars') |
1210 end; |
1216 end; |
1211 val (binds, vars') = fold_map pr ts vars; |
1217 val (binds, vars') = fold_map pr ts vars; |
1212 in Pretty.chunks [ |
1218 in |
1213 [str "(let", Pretty.fbrk, binds |> Pretty.chunks] |> Pretty.block, |
1219 Pretty.block_enclose ( |
1214 [str "in ", pr_term vars' NOBR t, str ")"] |> Pretty.block |
1220 str "let {", |
1215 ] end |
1221 Pretty.block [str "} in ", pr_term vars' NOBR t] |
|
1222 ) binds |
|
1223 end |
1216 | pr_term vars fxy (ICase ((td, _), bs)) = |
1224 | pr_term vars fxy (ICase ((td, _), bs)) = |
1217 let |
1225 let |
1218 fun pr (p, t) = |
1226 fun pr (p, t) = |
1219 let |
1227 let |
1220 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
1228 val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
1221 val vars' = CodegenThingol.intro_vars vs vars; |
1229 val vars' = CodegenThingol.intro_vars vs vars; |
1222 in |
1230 in |
1223 (Pretty.block o Pretty.breaks) [ |
1231 semicolon [ |
1224 pr_term vars' NOBR p, |
1232 pr_term vars' NOBR p, |
1225 str "->", |
1233 str "->", |
1226 pr_term vars' NOBR t |
1234 pr_term vars' NOBR t |
1227 ] |
1235 ] |
1228 end |
1236 end |
1229 in |
1237 in |
1230 (Pretty.enclose "(" ")" o Pretty.breaks) [ |
1238 Pretty.block_enclose ( |
1231 str "case", |
1239 concat [str "(case", pr_term vars NOBR td, str "of", str "{"], |
1232 pr_term vars NOBR td, |
1240 str "})" |
1233 str "of", |
1241 ) (map pr bs) |
1234 (Pretty.chunks o map pr) bs |
|
1235 ] |
|
1236 end |
1242 end |
1237 and pr_app' vars ((c, _), ts) = |
1243 and pr_app' vars ((c, _), ts) = |
1238 (str o deresolv) c :: map (pr_term vars BR) ts |
1244 (str o deresolv) c :: map (pr_term vars BR) ts |
1239 and pr_app vars fxy = |
1245 and pr_app vars fxy = |
1240 mk_app (pr_app' vars) (pr_term vars) const_syntax fxy; |
1246 mk_app (pr_app' vars) (pr_term vars) const_syntax fxy; |
1250 val vars = keyword_vars |
1256 val vars = keyword_vars |
1251 |> CodegenThingol.intro_vars consts |
1257 |> CodegenThingol.intro_vars consts |
1252 |> CodegenThingol.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
1258 |> CodegenThingol.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
1253 (insert (op =)) ts []); |
1259 (insert (op =)) ts []); |
1254 in |
1260 in |
1255 (Pretty.block o Pretty.breaks) ( |
1261 semicolon ( |
1256 (str o deresolv_here) name |
1262 (str o deresolv_here) name |
1257 :: map (pr_term vars BR) ts |
1263 :: map (pr_term vars BR) ts |
1258 @ [str "=", pr_term vars NOBR t] |
1264 @ str "=" |
|
1265 @@ pr_term vars NOBR t |
1259 ) |
1266 ) |
1260 end; |
1267 end; |
1261 in |
1268 in |
1262 Pretty.chunks ( |
1269 Pretty.chunks ( |
1263 Pretty.block [ |
1270 Pretty.block [ |
1264 (str o suffix " ::" o deresolv_here) name, |
1271 (str o suffix " ::" o deresolv_here) name, |
1265 Pretty.brk 1, |
1272 Pretty.brk 1, |
1266 pr_typscheme tyvars (vs, ty) |
1273 pr_typscheme tyvars (vs, ty), |
|
1274 str ";" |
1267 ] |
1275 ] |
1268 :: map pr_eq eqs |
1276 :: map pr_eq eqs |
1269 ) |
1277 ) |
1270 end |
1278 end |
1271 | pr_def (name, CodegenThingol.Datatype (vs, [(co, [ty])])) = |
1279 | pr_def (name, CodegenThingol.Datatype (vs, [(co, [ty])])) = |
1272 let |
1280 let |
1273 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1281 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1274 in |
1282 in |
1275 (Pretty.block o Pretty.breaks) ([ |
1283 semicolon ( |
1276 str "newtype", |
1284 str "newtype" |
1277 pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)), |
1285 :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
1278 str "=", |
1286 :: str "=" |
1279 (str o deresolv_here) co, |
1287 :: (str o deresolv_here) co |
1280 pr_typ tyvars BR ty |
1288 :: pr_typ tyvars BR ty |
1281 ] @ (if deriving_show name then [str "deriving (Read, Show)"] else [])) |
1289 :: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
|
1290 ) |
1282 end |
1291 end |
1283 | pr_def (name, CodegenThingol.Datatype (vs, co :: cos)) = |
1292 | pr_def (name, CodegenThingol.Datatype (vs, co :: cos)) = |
1284 let |
1293 let |
1285 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1294 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1286 fun pr_co (co, tys) = |
1295 fun pr_co (co, tys) = |
1287 (Pretty.block o Pretty.breaks) ( |
1296 concat ( |
1288 (str o deresolv_here) co |
1297 (str o deresolv_here) co |
1289 :: map (pr_typ tyvars BR) tys |
1298 :: map (pr_typ tyvars BR) tys |
1290 ) |
1299 ) |
1291 in |
1300 in |
1292 (Pretty.block o Pretty.breaks) (( |
1301 semicolon ( |
1293 str "data" |
1302 str "data" |
1294 :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
1303 :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
1295 :: str "=" |
1304 :: str "=" |
1296 :: pr_co co |
1305 :: pr_co co |
1297 :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos |
1306 :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos |
1298 ) @ (if deriving_show name then [str "deriving (Read, Show)"] else [])) |
1307 @ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
|
1308 ) |
1299 end |
1309 end |
1300 | pr_def (name, CodegenThingol.Class (superclasss, (v, classops))) = |
1310 | pr_def (name, CodegenThingol.Class (superclasss, (v, classops))) = |
1301 let |
1311 let |
1302 val tyvars = CodegenThingol.intro_vars [v] keyword_vars; |
1312 val tyvars = CodegenThingol.intro_vars [v] keyword_vars; |
1303 fun pr_classop (classop, ty) = |
1313 fun pr_classop (classop, ty) = |
1304 Pretty.block [ |
1314 semicolon [ |
1305 str (deresolv_here classop ^ " ::"), |
1315 (str o classop_name name) classop, |
1306 Pretty.brk 1, |
1316 str "::", |
1307 pr_typ tyvars NOBR ty |
1317 pr_typ tyvars NOBR ty |
1308 ] |
1318 ] |
1309 in |
1319 in |
1310 Pretty.block [ |
1320 Pretty.block_enclose ( |
1311 str "class ", |
1321 Pretty.block [ |
1312 pr_typparms tyvars [(v, superclasss)], |
1322 str "class ", |
1313 str (deresolv_here name ^ " " ^ CodegenThingol.lookup_var tyvars v), |
1323 pr_typparms tyvars [(v, superclasss)], |
1314 str " where", |
1324 str (deresolv_here name ^ " " ^ CodegenThingol.lookup_var tyvars v), |
1315 Pretty.fbrk, |
1325 str " where {" |
1316 Pretty.chunks (map pr_classop classops) |
1326 ], |
1317 ] |
1327 str "};" |
|
1328 ) (map pr_classop classops) |
1318 end |
1329 end |
1319 | pr_def (_, CodegenThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) = |
1330 | pr_def (_, CodegenThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) = |
1320 let |
1331 let |
1321 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1332 val tyvars = CodegenThingol.intro_vars (map fst vs) keyword_vars; |
1322 in |
1333 fun pr_instdef (classop, t) = |
1323 Pretty.block [ |
|
1324 str "instance ", |
|
1325 pr_typparms tyvars vs, |
|
1326 str (class_name class ^ " "), |
|
1327 pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
|
1328 str " where", |
|
1329 Pretty.fbrk, |
|
1330 Pretty.chunks (map (fn (classop, t) => |
|
1331 let |
1334 let |
1332 val consts = map_filter |
1335 val consts = map_filter |
1333 (fn c => if (is_some o const_syntax) c |
1336 (fn c => if (is_some o const_syntax) c |
1334 then NONE else (SOME o NameSpace.base o deresolv) c) |
1337 then NONE else (SOME o NameSpace.base o deresolv) c) |
1335 (CodegenThingol.fold_constnames (insert (op =)) t []); |
1338 (CodegenThingol.fold_constnames (insert (op =)) t []); |
1336 val vars = keyword_vars |
1339 val vars = keyword_vars |
1337 |> CodegenThingol.intro_vars consts; |
1340 |> CodegenThingol.intro_vars consts; |
1338 in |
1341 in |
1339 (Pretty.block o Pretty.breaks) [ |
1342 semicolon [ |
1340 (str o classop_name class) classop, |
1343 (str o classop_name class) classop, |
1341 str "=", |
1344 str "=", |
1342 pr_term vars NOBR t |
1345 pr_term vars NOBR t |
1343 ] |
1346 ] |
1344 end |
1347 end; |
1345 ) classop_defs) |
1348 in |
1346 ] |
1349 Pretty.block_enclose ( |
|
1350 Pretty.block [ |
|
1351 str "instance ", |
|
1352 pr_typparms tyvars vs, |
|
1353 str (class_name class ^ " "), |
|
1354 pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
|
1355 str " where {" |
|
1356 ], |
|
1357 str "};" |
|
1358 ) (map pr_instdef classop_defs) |
1347 end; |
1359 end; |
1348 in pr_def def end; |
1360 in pr_def def end; |
1349 |
1361 |
1350 val reserved_haskell = [ |
1362 val reserved_haskell = [ |
1351 "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
1363 "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
1421 andalso forall (deriv' tycos) tys |
1433 andalso forall (deriv' tycos) tys |
1422 | deriv' _ (ITyVar _) = true |
1434 | deriv' _ (ITyVar _) = true |
1423 in deriv [] tyco end; |
1435 in deriv [] tyco end; |
1424 fun seri_def qualified = pr_haskell class_syntax tyco_syntax const_syntax init_vars |
1436 fun seri_def qualified = pr_haskell class_syntax tyco_syntax const_syntax init_vars |
1425 deresolv_here (if qualified then deresolv else deresolv_here) (if string_classes then deriving_show else K false); |
1437 deresolv_here (if qualified then deresolv else deresolv_here) (if string_classes then deriving_show else K false); |
1426 fun write_module (SOME destination) modlname p = |
1438 fun write_module (SOME destination) modlname = |
1427 let |
1439 let |
1428 val filename = case modlname |
1440 val filename = case modlname |
1429 of "" => Path.explode "Main.hs" |
1441 of "" => Path.explode "Main.hs" |
1430 | _ => (Path.ext "hs" o Path.explode o implode o separate "/" o NameSpace.explode) modlname; |
1442 | _ => (Path.ext "hs" o Path.explode o implode o separate "/" o NameSpace.explode) modlname; |
1431 val pathname = Path.append destination filename; |
1443 val pathname = Path.append destination filename; |
1432 val _ = File.mkdir (Path.dir pathname); |
1444 val _ = File.mkdir (Path.dir pathname); |
1433 in File.write pathname (Pretty.setmp_margin 999999 Pretty.output p ^ "\n") end |
1445 in File.write pathname end |
1434 | write_module NONE _ p = |
1446 | write_module NONE _ = writeln; |
1435 writeln (Pretty.setmp_margin 999999 Pretty.output p ^ "\n"); |
|
1436 fun seri_module (modlname', (imports, (defs, _))) = |
1447 fun seri_module (modlname', (imports, (defs, _))) = |
1437 let |
1448 let |
1438 val imports' = |
1449 val imports' = |
1439 imports |
1450 imports |
1440 |> map (name_modl o fst o dest_name) |
1451 |> map (name_modl o fst o dest_name) |
1445 |> map_filter (try deresolv) |
1456 |> map_filter (try deresolv) |
1446 |> map NameSpace.base |
1457 |> map NameSpace.base |
1447 |> has_duplicates (op =); |
1458 |> has_duplicates (op =); |
1448 val mk_import = str o (if qualified |
1459 val mk_import = str o (if qualified |
1449 then prefix "import qualified " |
1460 then prefix "import qualified " |
1450 else prefix "import "); |
1461 else prefix "import ") o suffix ";"; |
1451 in |
1462 in |
1452 Pretty.chunks ( |
1463 Pretty.chunks ( |
1453 str ("module " ^ modlname' ^ " where") |
1464 str ("module " ^ modlname' ^ " where {") |
|
1465 :: str "" |
1454 :: map mk_import imports' |
1466 :: map mk_import imports' |
1455 @ ( |
1467 @ str "" |
1456 (case module_prolog modlname' |
1468 :: separate (str "") ((case module_prolog modlname' |
1457 of SOME prolog => [str "", prolog, str ""] |
1469 of SOME prolog => [prolog] |
1458 | NONE => [str ""]) |
1470 | NONE => []) |
1459 @ separate (str "") (map_filter |
1471 @ map_filter |
1460 (fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def)) |
1472 (fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def)) |
1461 | (_, (_, NONE)) => NONE) defs)) |
1473 | (_, (_, NONE)) => NONE) defs) |
1462 ) |> write_module destination modlname' |
1474 @ str "" |
|
1475 @@ str "}" |
|
1476 ) |
|
1477 |> code_output |
|
1478 |> write_module destination modlname' |
1463 end; |
1479 end; |
1464 in Symtab.fold (fn modl => fn () => seri_module modl) code' () end; |
1480 in Symtab.fold (fn modl => fn () => seri_module modl) code' () end; |
1465 |
1481 |
1466 val isar_seri_haskell = |
1482 val isar_seri_haskell = |
1467 parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) |
1483 parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) |