author | haftmann |
Mon, 12 Dec 2005 15:37:05 +0100 | |
changeset 18385 | d0071d93978e |
parent 18380 | 9668764224a7 |
child 18454 | 6720b5010a57 |
permissions | -rw-r--r-- |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_serializer.ML |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
5 |
Serializer from intermediate language ("Thin-gol") to |
18216 | 6 |
target languages (like ML or Haskell). |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
signature CODEGEN_SERIALIZER = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
sig |
18216 | 11 |
type primitives; |
12 |
val empty_prims: primitives; |
|
13 |
val add_prim: string * (string * string list) -> primitives -> primitives; |
|
14 |
val merge_prims: primitives * primitives -> primitives; |
|
15 |
val has_prim: primitives -> string -> bool; |
|
16 |
||
18304 | 17 |
type 'a pretty_syntax = string |
18 |
-> (int * (Pretty.T list -> ('a -> Pretty.T) -> Pretty.T)) option; |
|
19 |
type serializer = CodegenThingol.itype pretty_syntax -> CodegenThingol.iexpr pretty_syntax |
|
18217 | 20 |
-> primitives -> string list option -> CodegenThingol.module -> Pretty.T; |
18216 | 21 |
|
22 |
val ml_from_thingol: string list list -> string -> serializer; |
|
18282 | 23 |
val haskell_from_thingol: string list list -> string -> serializer; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
24 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
25 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
26 |
structure CodegenSerializer: CODEGEN_SERIALIZER = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
27 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
28 |
|
18216 | 29 |
open CodegenThingol; |
30 |
||
31 |
||
32 |
(** target language primitives **) |
|
33 |
||
34 |
type primitives = string Graph.T; |
|
35 |
||
36 |
val empty_prims = Graph.empty; |
|
37 |
||
38 |
fun add_prim (f, (def, deps)) prims = |
|
39 |
prims |
|
40 |
|> Graph.new_node (f, def) |
|
41 |
|> fold (fn dep => Graph.add_edge (f, dep)) deps; |
|
42 |
||
43 |
val merge_prims = Graph.merge (op =) : primitives * primitives -> primitives; |
|
44 |
||
45 |
val has_prim : primitives -> string -> bool = can o Graph.get_node; |
|
46 |
||
47 |
fun get_prims prims defs = |
|
48 |
defs |
|
49 |
|> filter (can (Graph.get_node prims)) |
|
50 |
|> `I |
|
51 |
||> Graph.all_succs prims |
|
52 |
||> (fn gr => Graph.subgraph gr prims) |
|
53 |
||> Graph.strong_conn |
|
54 |
||> rev |
|
55 |
||> Library.flat |
|
56 |
||> map (Graph.get_node prims) |
|
57 |
||> separate "" |
|
58 |
||> cat_lines |
|
59 |
||> suffix "\n"; |
|
60 |
||
61 |
fun mk_prims prims = get_prims prims (Graph.keys prims) |> snd; |
|
62 |
||
63 |
||
18304 | 64 |
(** keyword arguments **) |
65 |
||
66 |
type kw = (string * string option) list; |
|
67 |
fun kw_make args = |
|
68 |
let |
|
69 |
val parse_keyval = (); |
|
70 |
in |
|
71 |
Args.!!! (Scan.repeat ( |
|
72 |
Args.name |
|
73 |
-- Scan.option (Args.$$$ "=" |-- Args.name) |
|
74 |
) #> fst) args |
|
75 |
end; |
|
76 |
fun kw_get k kw = |
|
77 |
((the o AList.lookup (op =) kw) k, AList.delete (op =) k kw); |
|
78 |
fun kw_has kw k = |
|
79 |
AList.defined (op =) kw k; |
|
80 |
fun kw_done x [] = x |
|
81 |
| kw_done x kw = |
|
82 |
error ("uninterpreted keyword arguments: " ^ (commas o map (quote o fst)) kw); |
|
83 |
||
84 |
||
85 |
||
18216 | 86 |
(** generic serialization **) |
87 |
||
18304 | 88 |
type 'a pretty_syntax = string |
89 |
-> (int * (Pretty.T list -> ('a -> Pretty.T) -> Pretty.T)) option; |
|
90 |
type serializer = CodegenThingol.itype pretty_syntax -> CodegenThingol.iexpr pretty_syntax |
|
18217 | 91 |
-> primitives -> string list option -> CodegenThingol.module -> Pretty.T; |
18216 | 92 |
|
93 |
datatype lrx = L | R | X; |
|
94 |
||
95 |
datatype brack = |
|
96 |
BR |
|
97 |
| NOBR |
|
98 |
| INFX of (int * lrx); |
|
99 |
||
100 |
fun eval_lrx L L = false |
|
101 |
| eval_lrx R R = false |
|
102 |
| eval_lrx _ _ = true; |
|
103 |
||
104 |
fun eval_br BR _ = true |
|
105 |
| eval_br NOBR _ = false |
|
106 |
| eval_br (INFX (pr1, lr1)) (INFX (pr2, lr2)) = |
|
107 |
pr1 > pr2 |
|
108 |
orelse pr1 = pr2 |
|
109 |
andalso eval_lrx lr1 lr2 |
|
110 |
| eval_br (INFX _) _ = false; |
|
111 |
||
112 |
fun eval_br_postfix BR _ = false |
|
113 |
| eval_br_postfix NOBR _ = false |
|
114 |
| eval_br_postfix (INFX (pr1, lr1)) (INFX (pr2, lr2)) = |
|
115 |
pr1 > pr2 |
|
116 |
orelse pr1 = pr2 |
|
117 |
andalso eval_lrx lr1 lr2 |
|
118 |
| eval_br_postfix (INFX _) _ = false; |
|
119 |
||
120 |
fun brackify _ [p] = p |
|
121 |
| brackify true (ps as _::_) = Pretty.enclose "(" ")" (Pretty.breaks ps) |
|
122 |
| brackify false (ps as _::_) = Pretty.block (Pretty.breaks ps); |
|
123 |
||
124 |
fun postify [] f = [f] |
|
125 |
| postify [p] f = [p, Pretty.brk 1, f] |
|
126 |
| postify (ps as _::_) f = [Pretty.list "(" ")" ps, Pretty.brk 1, f]; |
|
127 |
||
18282 | 128 |
fun upper_first s = |
18335 | 129 |
let |
130 |
val (pr, b) = split_last (NameSpace.unpack s); |
|
131 |
val (c::cs) = String.explode b; |
|
132 |
in NameSpace.pack (pr @ [String.implode (Char.toUpper c :: cs)]) end; |
|
18282 | 133 |
|
134 |
fun lower_first s = |
|
18335 | 135 |
let |
136 |
val (pr, b) = split_last (NameSpace.unpack s); |
|
137 |
val (c::cs) = String.explode b; |
|
138 |
in NameSpace.pack (pr @ [String.implode (Char.toLower c :: cs)]) end; |
|
18282 | 139 |
|
140 |
||
18216 | 141 |
|
142 |
(** ML serializer **) |
|
143 |
||
144 |
local |
|
145 |
||
18385 | 146 |
fun ml_from_defs tyco_syntax const_syntax is_dicttype resolv ds = |
18216 | 147 |
let |
18282 | 148 |
fun chunk_defs ps = |
18216 | 149 |
let |
150 |
val (p_init, p_last) = split_last ps |
|
151 |
in |
|
152 |
Pretty.chunks (p_init @ [Pretty.block ([p_last, Pretty.str ";"])]) |
|
153 |
end; |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
154 |
val ml_label_uniq = translate_string (fn "_" => "__" | "." => "_" | c => c); |
18282 | 155 |
fun ml_from_type br (IType ("Pair", [t1, t2])) = |
18216 | 156 |
brackify (eval_br_postfix br (INFX (2, L))) [ |
18282 | 157 |
ml_from_type (INFX (2, X)) t1, |
18216 | 158 |
Pretty.str "*", |
18282 | 159 |
ml_from_type (INFX (2, X)) t2 |
18216 | 160 |
] |
18282 | 161 |
| ml_from_type br (IType ("Bool", [])) = |
18216 | 162 |
Pretty.str "bool" |
18282 | 163 |
| ml_from_type br (IType ("Integer", [])) = |
18216 | 164 |
Pretty.str "IntInf.int" |
18282 | 165 |
| ml_from_type br (IType ("List", [ty])) = |
166 |
postify ([ml_from_type BR ty]) (Pretty.str "list") |
|
18216 | 167 |
|> Pretty.block |
18282 | 168 |
| ml_from_type br (IType (tyco, typs)) = |
18216 | 169 |
let |
18282 | 170 |
val tyargs = (map (ml_from_type BR) typs) |
18216 | 171 |
in |
18304 | 172 |
case tyco_syntax tyco |
18216 | 173 |
of NONE => |
174 |
postify tyargs ((Pretty.str o resolv) tyco) |
|
175 |
|> Pretty.block |
|
18304 | 176 |
| SOME (i, pr) => |
18282 | 177 |
if i <> length (typs) |
178 |
then error "can only serialize strictly complete type applications to ML" |
|
18304 | 179 |
else pr tyargs (ml_from_type BR) |
18216 | 180 |
end |
18282 | 181 |
| ml_from_type br (IFun (t1, t2)) = |
18216 | 182 |
brackify (eval_br_postfix br (INFX (1, R))) [ |
18282 | 183 |
ml_from_type (INFX (1, X)) t1, |
18216 | 184 |
Pretty.str "->", |
18282 | 185 |
ml_from_type (INFX (1, R)) t2 |
18216 | 186 |
] |
18282 | 187 |
| ml_from_type _ (IVarT (v, [])) = |
18216 | 188 |
Pretty.str ("'" ^ v) |
18282 | 189 |
| ml_from_type _ (IVarT (_, sort)) = |
18216 | 190 |
"cannot serialize sort constrained type variable to ML: " ^ commas sort |> error |
18282 | 191 |
| ml_from_type _ (IDictT fs) = |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
192 |
Pretty.gen_list "," "{" "}" ( |
18216 | 193 |
map (fn (f, ty) => |
18282 | 194 |
Pretty.block [Pretty.str (ml_label_uniq f ^ ": "), ml_from_type NOBR ty]) fs |
18216 | 195 |
); |
196 |
fun ml_from_pat br (ICons (("True", []), _)) = |
|
197 |
Pretty.str "true" |
|
198 |
| ml_from_pat br (ICons (("False", []), _)) = |
|
199 |
Pretty.str "false" |
|
18282 | 200 |
| ml_from_pat br (ICons (("Pair", [p1, p2]), _)) = |
201 |
Pretty.list "(" ")" [ |
|
202 |
ml_from_pat NOBR p1, |
|
203 |
ml_from_pat NOBR p2 |
|
204 |
] |
|
18216 | 205 |
| ml_from_pat br (ICons (("Nil", []), _)) = |
206 |
Pretty.str "[]" |
|
207 |
| ml_from_pat br (p as ICons (("Cons", _), _)) = |
|
208 |
let |
|
209 |
fun dest_cons (ICons (("Cons", [ICons (("Pair", [p1, p2]), _)]), _)) = SOME (p1, p2) |
|
210 |
| dest_cons p = NONE |
|
211 |
in |
|
212 |
case unfoldr dest_cons p |
|
213 |
of (ps, (ICons (("Nil", []), _))) => |
|
214 |
ps |
|
215 |
|> map (ml_from_pat NOBR) |
|
216 |
|> Pretty.list "[" "]" |
|
217 |
| (ps, p) => |
|
218 |
(ps @ [p]) |
|
219 |
|> map (ml_from_pat (INFX (5, X))) |
|
220 |
|> separate (Pretty.str "::") |
|
221 |
|> brackify (eval_br br (INFX (5, R))) |
|
222 |
end |
|
223 |
| ml_from_pat br (ICons ((f, ps), ty)) = |
|
18335 | 224 |
(case const_syntax f |
225 |
of NONE => |
|
226 |
ps |
|
227 |
|> map (ml_from_pat BR) |
|
228 |
|> cons ((Pretty.str o resolv) f) |
|
229 |
|> brackify (eval_br br BR) |
|
230 |
| SOME (i, pr) => |
|
231 |
if i = length ps |
|
232 |
then |
|
233 |
pr (map (ml_from_pat BR) ps) (ml_from_expr BR) |
|
234 |
else |
|
235 |
error "number of argument mismatch in customary serialization") |
|
18216 | 236 |
| ml_from_pat br (IVarP (v, IType ("Integer", []))) = |
18282 | 237 |
brackify (eval_br br BR) [ |
238 |
Pretty.str v, |
|
239 |
Pretty.str ":", |
|
240 |
Pretty.str "IntInf.int" |
|
241 |
] |
|
18216 | 242 |
| ml_from_pat br (IVarP (v, _)) = |
18335 | 243 |
Pretty.str v |
244 |
and ml_from_expr br (e as (IApp (IConst ("Cons", _), _))) = |
|
18216 | 245 |
let |
246 |
fun dest_cons (IApp (IConst ("Cons", _), |
|
247 |
IApp (IApp (IConst ("Pair", _), e1), e2))) = SOME (e1, e2) |
|
248 |
| dest_cons p = NONE |
|
249 |
in |
|
250 |
case unfoldr dest_cons e |
|
251 |
of (es, (IConst ("Nil", _))) => |
|
252 |
es |
|
18282 | 253 |
|> map (ml_from_expr NOBR) |
18216 | 254 |
|> Pretty.list "[" "]" |
255 |
| (es, e) => |
|
256 |
(es @ [e]) |
|
18282 | 257 |
|> map (ml_from_expr (INFX (5, X))) |
18216 | 258 |
|> separate (Pretty.str "::") |
259 |
|> brackify (eval_br br (INFX (5, R))) |
|
260 |
end |
|
18282 | 261 |
| ml_from_expr br (e as IApp (e1, e2)) = |
18216 | 262 |
(case (unfold_app e) |
263 |
of (e as (IConst (f, _)), es) => |
|
264 |
ml_from_app br (f, es) |
|
18282 | 265 |
| _ => |
18216 | 266 |
brackify (eval_br br BR) [ |
18282 | 267 |
ml_from_expr NOBR e1, |
268 |
ml_from_expr BR e2 |
|
18216 | 269 |
]) |
18282 | 270 |
| ml_from_expr br (e as IConst (f, _)) = |
18216 | 271 |
ml_from_app br (f, []) |
18282 | 272 |
| ml_from_expr br (IVarE (v, _)) = |
18216 | 273 |
Pretty.str v |
18282 | 274 |
| ml_from_expr br (IAbs ((v, _), e)) = |
18216 | 275 |
brackify (eval_br br BR) [ |
276 |
Pretty.str ("fn " ^ v ^ " =>"), |
|
18282 | 277 |
ml_from_expr NOBR e |
18216 | 278 |
] |
18282 | 279 |
| ml_from_expr br (e as ICase (_, [_])) = |
18216 | 280 |
let |
281 |
val (ps, e) = unfold_let e; |
|
282 |
fun mk_val (p, e) = Pretty.block [ |
|
283 |
Pretty.str "val ", |
|
284 |
ml_from_pat BR p, |
|
285 |
Pretty.str " =", |
|
286 |
Pretty.brk 1, |
|
18282 | 287 |
ml_from_expr NOBR e, |
18216 | 288 |
Pretty.str ";" |
289 |
] |
|
290 |
in Pretty.chunks [ |
|
291 |
[Pretty.str ("let"), Pretty.fbrk, map mk_val ps |> Pretty.chunks] |> Pretty.block, |
|
18282 | 292 |
[Pretty.str ("in"), Pretty.fbrk, ml_from_expr NOBR e] |> Pretty.block, |
18216 | 293 |
Pretty.str ("end") |
294 |
] end |
|
18282 | 295 |
| ml_from_expr br (ICase (e, c::cs)) = |
18216 | 296 |
let |
297 |
fun mk_clause definer (p, e) = |
|
298 |
Pretty.block [ |
|
299 |
Pretty.str definer, |
|
300 |
ml_from_pat NOBR p, |
|
301 |
Pretty.str " =>", |
|
302 |
Pretty.brk 1, |
|
18282 | 303 |
ml_from_expr NOBR e |
18216 | 304 |
] |
305 |
in brackify (eval_br br BR) ( |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
306 |
Pretty.str "case" |
18282 | 307 |
:: ml_from_expr NOBR e |
18216 | 308 |
:: mk_clause "of " c |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
309 |
:: map (mk_clause "| ") cs |
18216 | 310 |
) end |
18282 | 311 |
| ml_from_expr br (IInst _) = |
18216 | 312 |
error "cannot serialize poly instant to ML" |
18282 | 313 |
| ml_from_expr br (IDictE fs) = |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
314 |
Pretty.gen_list "," "{" "}" ( |
18216 | 315 |
map (fn (f, e) => |
18282 | 316 |
Pretty.block [Pretty.str (ml_label_uniq f ^ " = "), ml_from_expr NOBR e]) fs |
18216 | 317 |
) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
318 |
| ml_from_expr br (ILookup ([], v)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
319 |
Pretty.str v |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
320 |
| ml_from_expr br (ILookup ([l], v)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
321 |
brackify (eval_br br BR) [ |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
322 |
Pretty.str ("#" ^ (ml_label_uniq l)), |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
323 |
Pretty.str v |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
324 |
] |
18282 | 325 |
| ml_from_expr br (ILookup (ls, v)) = |
18216 | 326 |
brackify (eval_br br BR) [ |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
327 |
Pretty.str ("(" |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
328 |
^ (ls |> map ((fn s => "#" ^ s) o ml_label_uniq) |> foldr1 (fn (l, e) => l ^ " o " ^ e)) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
329 |
^ ")"), |
18216 | 330 |
Pretty.str v |
331 |
] |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
332 |
| ml_from_expr _ e = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
333 |
error ("dubious expression: " ^ (Pretty.output o pretty_iexpr) e) |
18216 | 334 |
and mk_app_p br p args = |
335 |
brackify (eval_br br BR) |
|
18282 | 336 |
(p :: map (ml_from_expr BR) args) |
18216 | 337 |
and ml_from_app br ("Nil", []) = |
338 |
Pretty.str "[]" |
|
339 |
| ml_from_app br ("True", []) = |
|
340 |
Pretty.str "true" |
|
341 |
| ml_from_app br ("False", []) = |
|
342 |
Pretty.str "false" |
|
343 |
| ml_from_app br ("Pair", [e1, e2]) = |
|
344 |
Pretty.list "(" ")" [ |
|
18282 | 345 |
ml_from_expr NOBR e1, |
346 |
ml_from_expr NOBR e2 |
|
18216 | 347 |
] |
348 |
| ml_from_app br ("and", [e1, e2]) = |
|
349 |
brackify (eval_br br (INFX (~1, L))) [ |
|
18282 | 350 |
ml_from_expr (INFX (~1, L)) e1, |
18216 | 351 |
Pretty.str "andalso", |
18282 | 352 |
ml_from_expr (INFX (~1, X)) e2 |
18216 | 353 |
] |
354 |
| ml_from_app br ("or", [e1, e2]) = |
|
355 |
brackify (eval_br br (INFX (~2, L))) [ |
|
18282 | 356 |
ml_from_expr (INFX (~2, L)) e1, |
18216 | 357 |
Pretty.str "orelse", |
18282 | 358 |
ml_from_expr (INFX (~2, X)) e2 |
18216 | 359 |
] |
360 |
| ml_from_app br ("if", [b, e1, e2]) = |
|
361 |
brackify (eval_br br BR) [ |
|
362 |
Pretty.str "if", |
|
18282 | 363 |
ml_from_expr NOBR b, |
18216 | 364 |
Pretty.str "then", |
18282 | 365 |
ml_from_expr NOBR e1, |
18216 | 366 |
Pretty.str "else", |
18282 | 367 |
ml_from_expr NOBR e2 |
18216 | 368 |
] |
369 |
| ml_from_app br ("add", [e1, e2]) = |
|
370 |
brackify (eval_br br (INFX (6, L))) [ |
|
18282 | 371 |
ml_from_expr (INFX (6, L)) e1, |
18216 | 372 |
Pretty.str "+", |
18282 | 373 |
ml_from_expr (INFX (6, X)) e2 |
18216 | 374 |
] |
375 |
| ml_from_app br ("mult", [e1, e2]) = |
|
376 |
brackify (eval_br br (INFX (7, L))) [ |
|
18282 | 377 |
ml_from_expr (INFX (7, L)) e1, |
18216 | 378 |
Pretty.str "+", |
18282 | 379 |
ml_from_expr (INFX (7, X)) e2 |
18216 | 380 |
] |
381 |
| ml_from_app br ("lt", [e1, e2]) = |
|
382 |
brackify (eval_br br (INFX (4, L))) [ |
|
18282 | 383 |
ml_from_expr (INFX (4, L)) e1, |
18216 | 384 |
Pretty.str "<", |
18282 | 385 |
ml_from_expr (INFX (4, X)) e2 |
18216 | 386 |
] |
387 |
| ml_from_app br ("le", [e1, e2]) = |
|
388 |
brackify (eval_br br (INFX (7, L))) [ |
|
18282 | 389 |
ml_from_expr (INFX (4, L)) e1, |
18216 | 390 |
Pretty.str "<=", |
18282 | 391 |
ml_from_expr (INFX (4, X)) e2 |
18216 | 392 |
] |
393 |
| ml_from_app br ("minus", es) = |
|
394 |
mk_app_p br (Pretty.str "~") es |
|
395 |
| ml_from_app br ("wfrec", es) = |
|
396 |
mk_app_p br (Pretty.str "wfrec") es |
|
397 |
| ml_from_app br (f, es) = |
|
18304 | 398 |
case const_syntax f |
18282 | 399 |
of NONE => |
18335 | 400 |
(case es |
401 |
of [] => Pretty.str (resolv f) |
|
402 |
| es => |
|
403 |
let |
|
404 |
val (es', e) = split_last es; |
|
405 |
in mk_app_p br (ml_from_app NOBR (f, es')) [e] end) |
|
18304 | 406 |
| SOME (i, pr) => |
18282 | 407 |
let |
408 |
val (es1, es2) = splitAt (i, es); |
|
18304 | 409 |
in mk_app_p br (pr (map (ml_from_expr BR) es1) (ml_from_expr BR)) es2 end; |
18216 | 410 |
fun ml_from_funs (ds as d::ds_tl) = |
411 |
let |
|
18385 | 412 |
val _ = debug 15 (fn _ => "(1) FUN") (); |
18216 | 413 |
fun mk_definer [] = "val" |
414 |
| mk_definer _ = "fun" |
|
415 |
fun check_args (_, Fun ((pats, _)::_, _)) NONE = |
|
416 |
SOME (mk_definer pats) |
|
417 |
| check_args (_, Fun ((pats, _)::_, _)) (SOME definer) = |
|
418 |
if mk_definer pats = definer |
|
419 |
then SOME definer |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
420 |
else error ("mixing simultaneous vals and funs not implemented") |
18216 | 421 |
| check_args _ _ = |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
422 |
error ("function definition block containing other definitions than functions") |
18385 | 423 |
val _ = debug 15 (fn _ => "(2) FUN") (); |
18216 | 424 |
val definer = the (fold check_args ds NONE); |
18385 | 425 |
val _ = debug 15 (fn _ => "(3) FUN") (); |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
426 |
fun mk_eq definer f ty (pats, expr) = |
18216 | 427 |
let |
18385 | 428 |
val _ = debug 15 (fn _ => "(5) FUN") (); |
429 |
fun mk_pat_arg p = |
|
430 |
case itype_of_ipat p |
|
431 |
of ty as IType (tyco, _) => |
|
432 |
if is_dicttype tyco |
|
433 |
then Pretty.block [ |
|
434 |
Pretty.str "(", |
|
435 |
ml_from_pat NOBR p, |
|
436 |
Pretty.str ":", |
|
437 |
ml_from_type NOBR ty, |
|
438 |
Pretty.str ")" |
|
439 |
] |
|
440 |
else ml_from_pat BR p |
|
441 |
| _ => ml_from_pat BR p; |
|
442 |
val _ = debug 15 (fn _ => "(6) FUN") (); |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
443 |
val lhs = [Pretty.str (definer ^ " " ^ f)] |
18216 | 444 |
@ (if null pats |
18282 | 445 |
then [Pretty.str ":", ml_from_type NOBR ty] |
18385 | 446 |
else map mk_pat_arg pats) |
447 |
val _ = debug 15 (fn _ => "(7) FUN") (); |
|
18282 | 448 |
val rhs = [Pretty.str "=", ml_from_expr NOBR expr] |
18385 | 449 |
val _ = debug 15 (fn _ => "(8) FUN") (); |
18216 | 450 |
in |
451 |
Pretty.block (separate (Pretty.brk 1) (lhs @ rhs)) |
|
452 |
end |
|
453 |
fun mk_fun definer (f, Fun (eqs as eq::eq_tl, (_, ty))) = |
|
454 |
let |
|
18385 | 455 |
val _ = debug 15 (fn _ => "(4) FUN") (); |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
456 |
val (pats_hd::pats_tl) = (fst o split_list) eqs; |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
457 |
val shift = if null eq_tl then I else map (Pretty.block o single); |
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
458 |
in (Pretty.block o Pretty.fbreaks o shift) ( |
18216 | 459 |
mk_eq definer f ty eq |
460 |
:: map (mk_eq "|" f ty) eq_tl |
|
461 |
) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
462 |
end; |
18216 | 463 |
in |
464 |
chunk_defs ( |
|
465 |
mk_fun definer d |
|
466 |
:: map (mk_fun "and") ds_tl |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
467 |
) |> SOME |
18216 | 468 |
end; |
18282 | 469 |
fun ml_from_datatypes defs = |
18216 | 470 |
let |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
471 |
val defs' = List.mapPartial |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
472 |
(fn (name, Datatype info) => SOME (name, info) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
473 |
| (name, Datatypecons _) => NONE |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
474 |
| (name, def) => error ("datatype block containing illegal def: " ^ (Pretty.output o pretty_def) def) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
475 |
) ds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
476 |
fun praetify [] f = [f] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
477 |
| praetify [p] f = [f, Pretty.str " of ", p] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
478 |
fun mk_cons (co, typs) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
479 |
(Pretty.block oo praetify) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
480 |
(map (ml_from_type NOBR) typs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
481 |
(Pretty.str (resolv co)) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
482 |
fun mk_datatype definer (t, (vs, cs, _)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
483 |
Pretty.block ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
484 |
[Pretty.str definer] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
485 |
@ postify (map (ml_from_type BR o IVarT) vs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
486 |
(Pretty.str (resolv t)) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
487 |
@ [Pretty.str " =", |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
488 |
Pretty.brk 1] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
489 |
@ separate (Pretty.block [Pretty.brk 1, Pretty.str "| "]) (map mk_cons cs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
490 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
491 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
492 |
case defs' |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
493 |
of d::ds_tl => |
18216 | 494 |
chunk_defs ( |
495 |
mk_datatype "datatype " d |
|
496 |
:: map (mk_datatype "and ") ds_tl |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
497 |
) |> SOME |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
498 |
| _ => NONE |
18216 | 499 |
end; |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
500 |
fun ml_from_def (name, Nop) = |
18304 | 501 |
if exists (fn query => query name) |
502 |
[(fn name => (is_some o tyco_syntax) name), |
|
503 |
(fn name => (is_some o const_syntax) name)] |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
504 |
then NONE |
18216 | 505 |
else error ("empty statement during serialization: " ^ quote name) |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
506 |
| ml_from_def (name, Typesyn (vs, ty)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
507 |
(map (fn (vname, []) => () | _ => error "can't serialize sort constrained type declaration to ML") vs; |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
508 |
Pretty.block ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
509 |
Pretty.str "type " |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
510 |
:: postify (map (ml_from_type BR o IVarT) vs) (Pretty.str name) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
511 |
@ [Pretty.str " =", |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
512 |
Pretty.brk 1, |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
513 |
ml_from_type NOBR ty, |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
514 |
Pretty.str ";" |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
515 |
] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
516 |
)) |> SOME |
18216 | 517 |
| ml_from_def (name, Class _) = |
518 |
error ("can't serialize class declaration " ^ quote name ^ " to ML") |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
519 |
| ml_from_def (_, Classmember _) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
520 |
NONE |
18216 | 521 |
| ml_from_def (name, Classinst _) = |
522 |
error ("can't serialize instance declaration " ^ quote name ^ " to ML") |
|
18385 | 523 |
in (debug 10 (fn _ => "*** defs " ^ commas (map (fn (n, d) => n ^ " = " ^ (Pretty.output o pretty_def) d) ds)) (); |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
524 |
case ds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
525 |
of (_, Fun _)::_ => ml_from_funs ds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
526 |
| (_, Datatypecons _)::_ => ml_from_datatypes ds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
527 |
| (_, Datatype _)::_ => ml_from_datatypes ds |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
528 |
| [d] => ml_from_def d) |
18216 | 529 |
end; |
530 |
||
531 |
in |
|
532 |
||
18304 | 533 |
fun ml_from_thingol nspgrp name_root tyco_syntax const_syntax prims select module = |
18216 | 534 |
let |
18282 | 535 |
fun ml_validator name = |
536 |
let |
|
537 |
fun replace_invalid c = |
|
538 |
if (Char.isAlphaNum o the o Char.fromString) c orelse c = "'" |
|
539 |
andalso not (NameSpace.separator = c) |
|
540 |
then c |
|
541 |
else "_" |
|
542 |
fun suffix_it name = |
|
543 |
name |
|
544 |
|> member (op =) ThmDatabase.ml_reserved ? suffix "'" |
|
545 |
|> member (op =) CodegenThingol.prims ? suffix "'" |
|
546 |
|> has_prim prims ? suffix "'" |
|
547 |
|> (fn name' => if name = name' then name else suffix_it name') |
|
548 |
in |
|
549 |
name |
|
550 |
|> translate_string replace_invalid |
|
551 |
|> suffix_it |
|
552 |
|> (fn name' => if name = name' then NONE else SOME name') |
|
18360 | 553 |
end; |
18216 | 554 |
fun ml_from_module (name, ps) = |
555 |
Pretty.chunks ([ |
|
556 |
Pretty.str ("structure " ^ name ^ " = "), |
|
557 |
Pretty.str "struct", |
|
558 |
Pretty.str "" |
|
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
559 |
] @ separate (Pretty.str "") ps @ [ |
18216 | 560 |
Pretty.str "", |
561 |
Pretty.str ("end; (* struct " ^ name ^ " *)") |
|
562 |
]); |
|
18385 | 563 |
fun is_dicttype tyco = |
564 |
case get_def module tyco |
|
565 |
of Typesyn (_, IDictT _) => true |
|
566 |
| _ => false; |
|
18216 | 567 |
fun eta_expander "Pair" = 2 |
568 |
| eta_expander "Cons" = 2 |
|
569 |
| eta_expander "and" = 2 |
|
570 |
| eta_expander "or" = 2 |
|
571 |
| eta_expander "if" = 3 |
|
572 |
| eta_expander "add" = 2 |
|
573 |
| eta_expander "mult" = 2 |
|
574 |
| eta_expander "lt" = 2 |
|
575 |
| eta_expander "le" = 2 |
|
576 |
| eta_expander s = |
|
577 |
if NameSpace.is_qualified s |
|
578 |
then case get_def module s |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
579 |
of Datatypecons dtname => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
580 |
(case get_def module dtname |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
581 |
of Datatype (_, cs, _) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
582 |
let val l = AList.lookup (op =) cs s |> the |> length |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
583 |
in if l >= 2 then l else 0 end) |
18216 | 584 |
| _ => |
18304 | 585 |
const_syntax s |
586 |
|> Option.map fst |
|
18282 | 587 |
|> the_default 0 |
18385 | 588 |
else 0; |
18282 | 589 |
in |
18216 | 590 |
module |
18247
b17724cae935
code generator: case expressions, improved name resolving
haftmann
parents:
18231
diff
changeset
|
591 |
|> debug 12 (Pretty.output o pretty_module) |
18216 | 592 |
|> debug 3 (fn _ => "selecting submodule...") |
18217 | 593 |
|> (if is_some select then (partof o the) select else I) |
18216 | 594 |
|> debug 3 (fn _ => "eta-expanding...") |
595 |
|> eta_expand eta_expander |
|
18231 | 596 |
|> debug 3 (fn _ => "eta-expanding polydefs...") |
597 |
|> eta_expand_poly |
|
18216 | 598 |
|> debug 3 (fn _ => "tupelizing datatypes...") |
599 |
|> tupelize_cons |
|
600 |
|> debug 3 (fn _ => "eliminating classes...") |
|
601 |
|> eliminate_classes |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
602 |
|> debug 12 (Pretty.output o pretty_module) |
18385 | 603 |
|> debug 3 (fn _ => "serializing...") |
604 |
|> serialize (ml_from_defs tyco_syntax const_syntax is_dicttype) ml_from_module ml_validator nspgrp name_root |
|
18361 | 605 |
|> (fn p => Pretty.chunks [setmp print_mode [] (Pretty.str o mk_prims) prims, p]) |
18216 | 606 |
end; |
607 |
||
18231 | 608 |
fun ml_from_thingol' nspgrp name_root = |
609 |
Scan.optional ( |
|
610 |
OuterParse.$$$ "(" |-- OuterParse.list1 OuterParse.text --| OuterParse.$$$ ")" |
|
611 |
) [] |
|
612 |
>> (fn _ => ml_from_thingol nspgrp name_root); |
|
613 |
||
18216 | 614 |
(* ML infix precedence |
615 |
7 / * div mod |
|
616 |
6 + - ^ |
|
617 |
5 :: @ |
|
618 |
4 = <> < > <= >= |
|
619 |
3 := o *) |
|
620 |
||
621 |
end; (* local *) |
|
622 |
||
18282 | 623 |
local |
624 |
||
18304 | 625 |
fun haskell_from_defs tyco_syntax const_syntax is_cons resolv defs = |
18282 | 626 |
let |
627 |
val resolv = fn s => |
|
628 |
let |
|
629 |
val (prfix, base) = (split_last o NameSpace.unpack o resolv) s |
|
630 |
in |
|
631 |
NameSpace.pack (map upper_first prfix @ [base]) |
|
632 |
end; |
|
633 |
fun resolv_const f = |
|
18335 | 634 |
if NameSpace.is_qualified f |
635 |
then |
|
636 |
if is_cons f |
|
637 |
then (upper_first o resolv) f |
|
638 |
else (lower_first o resolv) f |
|
639 |
else |
|
640 |
f; |
|
18282 | 641 |
fun haskell_from_sctxt vs = |
642 |
let |
|
643 |
fun from_sctxt [] = Pretty.str "" |
|
644 |
| from_sctxt vs = |
|
645 |
vs |
|
646 |
|> map (fn (v, cls) => Pretty.str ((upper_first o resolv) cls ^ " " ^ lower_first v)) |
|
647 |
|> Pretty.gen_list "," "(" ")" |
|
648 |
|> (fn p => Pretty.block [p, Pretty.str " => "]) |
|
649 |
in |
|
650 |
vs |
|
651 |
|> map (fn (v, sort) => map (pair v) sort) |
|
652 |
|> Library.flat |
|
653 |
|> from_sctxt |
|
654 |
end; |
|
655 |
fun haskell_from_type br ty = |
|
656 |
let |
|
657 |
fun from_itype br (IType ("Pair", [t1, t2])) sctxt = |
|
658 |
sctxt |
|
659 |
|> from_itype NOBR t1 |
|
660 |
||>> from_itype NOBR t2 |
|
661 |
|-> (fn (p1, p2) => pair (Pretty.gen_list "," "(" ")" [p1, p2])) |
|
662 |
| from_itype br (IType ("List", [ty])) sctxt = |
|
663 |
sctxt |
|
664 |
|> from_itype NOBR ty |
|
665 |
|-> (fn p => pair (Pretty.enclose "[" "]" [p])) |
|
666 |
| from_itype br (IType (tyco, tys)) sctxt = |
|
667 |
let |
|
668 |
fun mk_itype NONE tyargs sctxt = |
|
669 |
sctxt |
|
670 |
|> pair (brackify (eval_br br BR) ((Pretty.str o upper_first o resolv) tyco :: tyargs)) |
|
18304 | 671 |
| mk_itype (SOME (i, pr)) tyargs sctxt = |
18282 | 672 |
if i <> length (tys) |
673 |
then error "can only serialize strictly complete type applications to haskell" |
|
674 |
else |
|
675 |
sctxt |
|
18304 | 676 |
|> pair (pr tyargs (haskell_from_type BR)) |
18282 | 677 |
in |
678 |
sctxt |
|
679 |
|> fold_map (from_itype BR) tys |
|
18304 | 680 |
|-> mk_itype (tyco_syntax tyco) |
18282 | 681 |
end |
682 |
| from_itype br (IFun (t1, t2)) sctxt = |
|
683 |
sctxt |
|
684 |
|> from_itype (INFX (1, X)) t1 |
|
685 |
||>> from_itype (INFX (1, R)) t2 |
|
686 |
|-> (fn (p1, p2) => pair ( |
|
687 |
brackify (eval_br br (INFX (1, R))) [ |
|
688 |
p1, |
|
689 |
Pretty.str "->", |
|
690 |
p2 |
|
691 |
] |
|
692 |
)) |
|
693 |
| from_itype br (IVarT (v, [])) sctxt = |
|
694 |
sctxt |
|
695 |
|> pair ((Pretty.str o lower_first) v) |
|
696 |
| from_itype br (IVarT (v, sort)) sctxt = |
|
697 |
sctxt |
|
698 |
|> AList.default (op =) (v, []) |
|
699 |
|> AList.map_entry (op =) v (fold (insert (op =)) sort) |
|
700 |
|> pair ((Pretty.str o lower_first) v) |
|
701 |
| from_itype br (IDictT _) _ = |
|
702 |
error "cannot serialize dictionary type to haskell" |
|
703 |
in |
|
704 |
[] |
|
705 |
|> from_itype br ty |
|
706 |
||> haskell_from_sctxt |
|
707 |
|> (fn (pty, pctxt) => Pretty.block [pctxt, pty]) |
|
708 |
end; |
|
709 |
fun haskell_from_pat br (ICons (("Pair", [p1, p2]), _)) = |
|
710 |
Pretty.list "(" ")" [ |
|
711 |
haskell_from_pat NOBR p1, |
|
712 |
haskell_from_pat NOBR p2 |
|
713 |
] |
|
714 |
| haskell_from_pat br (ICons (("Nil", []), _)) = |
|
715 |
Pretty.str "[]" |
|
716 |
| haskell_from_pat br (p as ICons (("Cons", _), _)) = |
|
717 |
let |
|
718 |
fun dest_cons (ICons (("Cons", [p1, p2]), ty)) = SOME (p1, p2) |
|
719 |
| dest_cons p = NONE |
|
720 |
in |
|
721 |
case unfoldr dest_cons p |
|
722 |
of (ps, (ICons (("Nil", []), _))) => |
|
723 |
ps |
|
724 |
|> map (haskell_from_pat NOBR) |
|
725 |
|> Pretty.list "[" "]" |
|
726 |
| (ps, p) => |
|
727 |
(ps @ [p]) |
|
728 |
|> map (haskell_from_pat (INFX (5, X))) |
|
729 |
|> separate (Pretty.str ":") |
|
730 |
|> brackify (eval_br br (INFX (5, R))) |
|
731 |
end |
|
732 |
| haskell_from_pat br (ICons ((f, ps), _)) = |
|
18335 | 733 |
(case const_syntax f |
734 |
of NONE => |
|
735 |
ps |
|
736 |
|> map (haskell_from_pat BR) |
|
737 |
|> cons ((Pretty.str o resolv_const) f) |
|
738 |
|> brackify (eval_br br BR) |
|
739 |
| SOME (i, pr) => |
|
740 |
if i = length ps |
|
741 |
then |
|
742 |
pr (map (haskell_from_pat BR) ps) (haskell_from_expr BR) |
|
743 |
else |
|
744 |
error "number of argument mismatch in customary serialization") |
|
18282 | 745 |
| haskell_from_pat br (IVarP (v, _)) = |
18335 | 746 |
(Pretty.str o lower_first) v |
747 |
and haskell_from_expr br (e as (IApp (IApp (IConst ("Cons", _), _), _))) = |
|
18282 | 748 |
let |
749 |
fun dest_cons (IApp (IApp (IConst ("Cons", _), e1), e2)) = SOME (e1, e2) |
|
750 |
| dest_cons p = NONE |
|
751 |
in |
|
752 |
case unfoldr dest_cons e |
|
753 |
of (es, (IConst ("Nil", _))) => |
|
754 |
es |
|
755 |
|> map (haskell_from_expr NOBR) |
|
756 |
|> Pretty.list "[" "]" |
|
757 |
| (es, e) => |
|
758 |
(es @ [e]) |
|
759 |
|> map (haskell_from_expr (INFX (5, X))) |
|
760 |
|> separate (Pretty.str ":") |
|
761 |
|> brackify (eval_br br (INFX (5, R))) |
|
762 |
end |
|
763 |
| haskell_from_expr br (e as IApp (e1, e2)) = |
|
764 |
(case (unfold_app e) |
|
765 |
of (e as (IConst (f, _)), es) => |
|
766 |
haskell_from_app br (f, es) |
|
767 |
| _ => |
|
768 |
brackify (eval_br br BR) [ |
|
769 |
haskell_from_expr NOBR e1, |
|
770 |
haskell_from_expr BR e2 |
|
771 |
]) |
|
772 |
| haskell_from_expr br (e as IConst (f, _)) = |
|
773 |
haskell_from_app br (f, []) |
|
774 |
| haskell_from_expr br (IVarE (v, _)) = |
|
775 |
(Pretty.str o lower_first) v |
|
776 |
| haskell_from_expr br (e as IAbs _) = |
|
777 |
let |
|
778 |
val (vs, body) = unfold_abs e |
|
779 |
in |
|
780 |
brackify (eval_br br BR) ( |
|
18361 | 781 |
Pretty.str "\\" |
18282 | 782 |
:: map (Pretty.str o lower_first o fst) vs @ [ |
783 |
Pretty.str "->", |
|
784 |
haskell_from_expr NOBR body |
|
785 |
]) |
|
786 |
end |
|
787 |
| haskell_from_expr br (e as ICase (_, [_])) = |
|
788 |
let |
|
789 |
val (ps, body) = unfold_let e; |
|
790 |
fun mk_bind (p, e) = Pretty.block [ |
|
791 |
haskell_from_pat BR p, |
|
792 |
Pretty.str " =", |
|
793 |
Pretty.brk 1, |
|
794 |
haskell_from_expr NOBR e |
|
795 |
]; |
|
796 |
in Pretty.chunks [ |
|
797 |
[Pretty.str ("let"), Pretty.fbrk, map mk_bind ps |> Pretty.chunks] |> Pretty.block, |
|
798 |
[Pretty.str ("in "), haskell_from_expr NOBR body] |> Pretty.block |
|
799 |
] end |
|
800 |
| haskell_from_expr br (ICase (e, c::cs)) = |
|
801 |
let |
|
802 |
fun mk_clause (p, e) = |
|
803 |
Pretty.block [ |
|
804 |
haskell_from_pat NOBR p, |
|
805 |
Pretty.str " ->", |
|
806 |
Pretty.brk 1, |
|
807 |
haskell_from_expr NOBR e |
|
808 |
] |
|
809 |
in (Pretty.block o Pretty.fbreaks) ( |
|
810 |
Pretty.block [Pretty.str "case ", haskell_from_expr NOBR e, Pretty.str " of"] |
|
811 |
:: map (mk_clause) cs |
|
812 |
)end |
|
813 |
| haskell_from_expr br (IInst (e, _)) = |
|
814 |
haskell_from_expr br e |
|
815 |
| haskell_from_expr br (IDictE _) = |
|
816 |
error "cannot serialize dictionary expression to haskell" |
|
817 |
| haskell_from_expr br (ILookup _) = |
|
818 |
error "cannot serialize lookup expression to haskell" |
|
819 |
and mk_app_p br p args = |
|
820 |
brackify (eval_br br BR) |
|
821 |
(p :: map (haskell_from_expr BR) args) |
|
822 |
and haskell_from_app br ("Nil", []) = |
|
823 |
Pretty.str "[]" |
|
824 |
| haskell_from_app br ("Cons", es) = |
|
825 |
mk_app_p br (Pretty.str "(:)") es |
|
18335 | 826 |
| haskell_from_app br ("eq", [e1, e2]) = |
827 |
brackify (eval_br br (INFX (4, L))) [ |
|
828 |
haskell_from_expr (INFX (4, L)) e1, |
|
829 |
Pretty.str "==", |
|
830 |
haskell_from_expr (INFX (4, X)) e2 |
|
831 |
] |
|
18282 | 832 |
| haskell_from_app br ("Pair", [e1, e2]) = |
833 |
Pretty.list "(" ")" [ |
|
834 |
haskell_from_expr NOBR e1, |
|
835 |
haskell_from_expr NOBR e2 |
|
836 |
] |
|
837 |
| haskell_from_app br ("if", [b, e1, e2]) = |
|
838 |
brackify (eval_br br BR) [ |
|
839 |
Pretty.str "if", |
|
840 |
haskell_from_expr NOBR b, |
|
841 |
Pretty.str "then", |
|
842 |
haskell_from_expr NOBR e1, |
|
843 |
Pretty.str "else", |
|
844 |
haskell_from_expr NOBR e2 |
|
845 |
] |
|
18335 | 846 |
| haskell_from_app br ("and", es) = |
847 |
haskell_from_binop br 3 R "&&" es |
|
848 |
| haskell_from_app br ("or", es) = |
|
849 |
haskell_from_binop br 2 R "||" es |
|
850 |
| haskell_from_app br ("add", es) = |
|
851 |
haskell_from_binop br 6 L "+" es |
|
852 |
| haskell_from_app br ("mult", es) = |
|
853 |
haskell_from_binop br 7 L "*" es |
|
854 |
| haskell_from_app br ("lt", es) = |
|
855 |
haskell_from_binop br 4 L "<" es |
|
856 |
| haskell_from_app br ("le", es) = |
|
857 |
haskell_from_binop br 4 L "<=" es |
|
18282 | 858 |
| haskell_from_app br ("minus", es) = |
859 |
mk_app_p br (Pretty.str "negate") es |
|
860 |
| haskell_from_app br ("wfrec", es) = |
|
861 |
mk_app_p br (Pretty.str "wfrec") es |
|
862 |
| haskell_from_app br (f, es) = |
|
18304 | 863 |
case const_syntax f |
18282 | 864 |
of NONE => |
18335 | 865 |
(case es |
866 |
of [] => Pretty.str (resolv_const f) |
|
867 |
| es => |
|
868 |
let |
|
869 |
val (es', e) = split_last es; |
|
870 |
in mk_app_p br (haskell_from_app NOBR (f, es')) [e] end) |
|
18304 | 871 |
| SOME (i, pr) => |
18282 | 872 |
let |
873 |
val (es1, es2) = splitAt (i, es); |
|
18335 | 874 |
in mk_app_p br (pr (map (haskell_from_expr BR) es1) (haskell_from_expr BR)) es2 end |
875 |
and haskell_from_binop br pr L f [e1, e2] = |
|
876 |
brackify (eval_br br (INFX (pr, L))) [ |
|
877 |
haskell_from_expr (INFX (pr, L)) e1, |
|
878 |
Pretty.str f, |
|
879 |
haskell_from_expr (INFX (pr, X)) e2 |
|
880 |
] |
|
881 |
| haskell_from_binop br pr R f [e1, e2] = |
|
882 |
brackify (eval_br br (INFX (pr, R))) [ |
|
883 |
haskell_from_expr (INFX (pr, X)) e1, |
|
884 |
Pretty.str f, |
|
885 |
haskell_from_expr (INFX (pr, R)) e2 |
|
886 |
] |
|
887 |
| haskell_from_binop br pr ass f args = |
|
888 |
mk_app_p br (Pretty.str ("(" ^ f ^ ")")) args |
|
18282 | 889 |
fun haskell_from_def (name, Nop) = |
18304 | 890 |
if exists (fn query => query name) |
891 |
[(fn name => (is_some o tyco_syntax) name), |
|
892 |
(fn name => (is_some o const_syntax) name)] |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
893 |
then NONE |
18282 | 894 |
else error ("empty statement during serialization: " ^ quote name) |
895 |
| haskell_from_def (name, Fun (eqs, (_, ty))) = |
|
896 |
let |
|
18385 | 897 |
val _ = print "(1) FUN"; |
18282 | 898 |
fun from_eq name (args, rhs) = |
18385 | 899 |
(print args; print rhs; |
18282 | 900 |
Pretty.block [ |
901 |
Pretty.str (lower_first name), |
|
902 |
Pretty.block (map (fn p => Pretty.block [Pretty.brk 1, haskell_from_pat BR p]) args), |
|
903 |
Pretty.brk 1, |
|
904 |
Pretty.str ("="), |
|
905 |
Pretty.brk 1, |
|
906 |
haskell_from_expr NOBR rhs |
|
18385 | 907 |
]) |
908 |
val _ = print "(2) FUN"; |
|
18282 | 909 |
in |
910 |
Pretty.chunks [ |
|
911 |
Pretty.block [ |
|
912 |
Pretty.str (name ^ " ::"), |
|
913 |
Pretty.brk 1, |
|
914 |
haskell_from_type NOBR ty |
|
915 |
], |
|
916 |
Pretty.chunks (map (from_eq name) eqs) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
917 |
] |> SOME |
18282 | 918 |
end |
919 |
| haskell_from_def (name, Typesyn (vs, ty)) = |
|
920 |
Pretty.block [ |
|
921 |
Pretty.str "type ", |
|
922 |
haskell_from_sctxt vs, |
|
923 |
Pretty.str (upper_first name), |
|
924 |
Pretty.block (map (fn (v, _) => Pretty.str (" " ^ (lower_first) v)) vs), |
|
925 |
Pretty.str " =", |
|
926 |
Pretty.brk 1, |
|
927 |
haskell_from_type NOBR ty |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
928 |
] |> SOME |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
929 |
| haskell_from_def (name, Datatype (vars, constrs, _)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
930 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
931 |
fun mk_cons (co, tys) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
932 |
(Pretty.block o Pretty.breaks) ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
933 |
Pretty.str ((upper_first o resolv) co) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
934 |
:: map (haskell_from_type NOBR) tys |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
935 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
936 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
937 |
Pretty.block ( |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
938 |
Pretty.str "data " |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
939 |
:: haskell_from_sctxt vars |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
940 |
:: Pretty.str (upper_first name) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
941 |
:: Pretty.block (map (fn (v, _) => Pretty.str (" " ^ (lower_first) v)) vars) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
942 |
:: Pretty.str " =" |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
943 |
:: Pretty.brk 1 |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
944 |
:: separate (Pretty.block [Pretty.brk 1, Pretty.str "| "]) (map mk_cons constrs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
945 |
) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
946 |
end |> SOME |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
947 |
| haskell_from_def (_, Datatypecons _) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
948 |
NONE |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
949 |
| haskell_from_def (name, Class (supclasss, v, membrs, _)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
950 |
let |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
951 |
fun mk_member (m, (_, ty)) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
952 |
Pretty.block [ |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
953 |
Pretty.str (resolv m ^ " ::"), |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
954 |
Pretty.brk 1, |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
955 |
haskell_from_type NOBR ty |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
956 |
] |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
957 |
in |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
958 |
Pretty.block [ |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
959 |
Pretty.str "class ", |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
960 |
haskell_from_sctxt (map (fn class => (v, [class])) supclasss), |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
961 |
Pretty.str ((upper_first name) ^ " " ^ v), |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
962 |
Pretty.str " where", |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
963 |
Pretty.fbrk, |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
964 |
Pretty.chunks (map mk_member membrs) |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
965 |
] |> SOME |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
966 |
end |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
967 |
| haskell_from_def (name, Classmember _) = |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
968 |
NONE |
18385 | 969 |
| haskell_from_def (_, Classinst ("Eq", (tyco, arity), [(_, eqpred)])) = |
970 |
Pretty.block [ |
|
971 |
Pretty.str "instance ", |
|
972 |
haskell_from_sctxt arity, |
|
973 |
Pretty.str "Eq", |
|
974 |
Pretty.str " ", |
|
975 |
haskell_from_type NOBR (IType (tyco, (map (IVarT o rpair [] o fst)) arity)), |
|
976 |
Pretty.str " where", |
|
977 |
Pretty.fbrk, |
|
978 |
Pretty.str ("(==) = " ^ (lower_first o resolv) eqpred) |
|
979 |
] |> SOME |
|
18282 | 980 |
| haskell_from_def (_, Classinst (clsname, (tyco, arity), instmems)) = |
981 |
Pretty.block [ |
|
982 |
Pretty.str "instance ", |
|
983 |
haskell_from_sctxt arity, |
|
984 |
Pretty.str ((upper_first o resolv) clsname), |
|
985 |
Pretty.str " ", |
|
18360 | 986 |
haskell_from_type NOBR (IType (tyco, (map (IVarT o rpair [] o fst)) arity)), |
18282 | 987 |
Pretty.str " where", |
988 |
Pretty.fbrk, |
|
989 |
Pretty.chunks (map (fn (member, const) => |
|
990 |
Pretty.str ((lower_first o resolv) member ^ " = " ^ (lower_first o resolv) const) |
|
991 |
) instmems) |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
992 |
] |> SOME |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
993 |
in |
18385 | 994 |
case List.mapPartial (fn (name, def) => (print ("serializing " ^ name); haskell_from_def (name, def))) defs |
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
995 |
of [] => NONE |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
996 |
| l => (SOME o Pretty.block) l |
18282 | 997 |
end; |
998 |
||
999 |
in |
|
1000 |
||
18304 | 1001 |
fun haskell_from_thingol nspgrp name_root tyco_syntax const_syntax prims select module = |
18282 | 1002 |
let |
1003 |
fun haskell_from_module (name, ps) = |
|
1004 |
Pretty.block [ |
|
1005 |
Pretty.str ("module " ^ (upper_first name) ^ " where"), |
|
1006 |
Pretty.fbrk, |
|
1007 |
Pretty.fbrk, |
|
1008 |
Pretty.chunks (separate (Pretty.str "") ps) |
|
1009 |
]; |
|
18360 | 1010 |
fun haskell_validator name = |
1011 |
let |
|
1012 |
fun replace_invalid c = |
|
1013 |
if (Char.isAlphaNum o the o Char.fromString) c orelse c = "'" |
|
1014 |
andalso not (NameSpace.separator = c) |
|
1015 |
then c |
|
1016 |
else "_" |
|
1017 |
fun suffix_it name = |
|
1018 |
name |
|
1019 |
|> member (op =) CodegenThingol.prims ? suffix "'" |
|
1020 |
|> has_prim prims ? suffix "'" |
|
1021 |
|> (fn name' => if name = name' then name else suffix_it name') |
|
1022 |
in |
|
1023 |
name |
|
1024 |
|> translate_string replace_invalid |
|
1025 |
|> suffix_it |
|
1026 |
|> (fn name' => if name = name' then NONE else SOME name') |
|
1027 |
end; |
|
18282 | 1028 |
fun eta_expander "Pair" = 2 |
1029 |
| eta_expander "if" = 3 |
|
1030 |
| eta_expander s = |
|
1031 |
if NameSpace.is_qualified s |
|
1032 |
then case get_def module s |
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1033 |
of Datatypecons dtname => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1034 |
(case get_def module dtname |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1035 |
of Datatype (_, cs, _) => |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1036 |
let val l = AList.lookup (op =) cs s |> the |> length |
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18361
diff
changeset
|
1037 |
in if l >= 2 then l else 0 end) |
18282 | 1038 |
| _ => |
18304 | 1039 |
const_syntax s |
1040 |
|> Option.map fst |
|
18282 | 1041 |
|> the_default 0 |
1042 |
else 0; |
|
1043 |
fun is_cons f = |
|
1044 |
NameSpace.is_qualified f |
|
1045 |
andalso case get_def module f |
|
1046 |
of Datatypecons _ => true |
|
1047 |
| _ => false; |
|
1048 |
in |
|
1049 |
module |
|
1050 |
|> debug 12 (Pretty.output o pretty_module) |
|
1051 |
|> debug 3 (fn _ => "selecting submodule...") |
|
1052 |
|> (if is_some select then (partof o the) select else I) |
|
1053 |
|> debug 3 (fn _ => "eta-expanding...") |
|
1054 |
|> eta_expand eta_expander |
|
18385 | 1055 |
|> debug 3 (fn _ => "serializing...") |
18282 | 1056 |
|> serialize (haskell_from_defs tyco_syntax const_syntax is_cons) haskell_from_module haskell_validator nspgrp name_root |
18361 | 1057 |
|> (fn p => Pretty.chunks [setmp print_mode [] (Pretty.str o mk_prims) prims, p]) |
18282 | 1058 |
end; |
1059 |
||
1060 |
end; (* local *) |
|
1061 |
||
18216 | 1062 |
end; (* struct *) |
1063 |