adamc@3
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@3
|
2 * All rights reserved.
|
adamc@3
|
3 *
|
adamc@3
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@3
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@3
|
6 *
|
adamc@3
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@3
|
8 * this list of conditions and the following disclaimer.
|
adamc@3
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@3
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@3
|
11 * and/or other materials provided with the distribution.
|
adamc@3
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@3
|
13 * derived from this software without specific prior written permission.
|
adamc@3
|
14 *
|
adamc@3
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@3
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@3
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@3
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@3
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@3
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@3
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@3
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@3
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@3
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@3
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@3
|
26 *)
|
adamc@3
|
27
|
adamc@244
|
28 (* Pretty-printing elaborated Ur/Web *)
|
adamc@3
|
29
|
adamc@3
|
30 structure ElabPrint :> ELAB_PRINT = struct
|
adamc@3
|
31
|
adamc@3
|
32 open Print.PD
|
adamc@3
|
33 open Print
|
adamc@3
|
34
|
adamc@3
|
35 open Elab
|
adamc@3
|
36
|
adamc@3
|
37 structure E = ElabEnv
|
adamc@3
|
38
|
adamc@11
|
39 val debug = ref false
|
adamc@11
|
40
|
adamc@3
|
41 fun p_kind' par (k, _) =
|
adamc@3
|
42 case k of
|
adamc@3
|
43 KType => string "Type"
|
adamc@3
|
44 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
|
adamc@3
|
45 space,
|
adamc@3
|
46 string "->",
|
adamc@3
|
47 space,
|
adamc@3
|
48 p_kind k2])
|
adamc@3
|
49 | KName => string "Name"
|
adamc@3
|
50 | KRecord k => box [string "{", p_kind k, string "}"]
|
adamc@82
|
51 | KUnit => string "Unit"
|
adamc@207
|
52 | KTuple ks => box [string "(",
|
adamc@207
|
53 p_list_sep (box [space, string "*", space]) p_kind ks,
|
adamc@207
|
54 string ")"]
|
adamc@3
|
55
|
adamc@3
|
56 | KError => string "<ERROR>"
|
adamc@76
|
57 | KUnif (_, _, ref (SOME k)) => p_kind' par k
|
adamc@76
|
58 | KUnif (_, s, _) => string ("<UNIF:" ^ s ^ ">")
|
adamc@3
|
59
|
adamc@3
|
60 and p_kind k = p_kind' false k
|
adamc@3
|
61
|
adamc@3
|
62 fun p_explicitness e =
|
adamc@3
|
63 case e of
|
adamc@3
|
64 Explicit => string "::"
|
adamc@3
|
65 | Implicit => string ":::"
|
adamc@3
|
66
|
adamc@3
|
67 fun p_con' par env (c, _) =
|
adamc@3
|
68 case c of
|
adamc@3
|
69 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
|
adamc@3
|
70 space,
|
adamc@3
|
71 string "->",
|
adamc@3
|
72 space,
|
adamc@3
|
73 p_con env t2])
|
adamc@3
|
74 | TCFun (e, x, k, c) => parenIf par (box [string x,
|
adamc@3
|
75 space,
|
adamc@3
|
76 p_explicitness e,
|
adamc@3
|
77 space,
|
adamc@3
|
78 p_kind k,
|
adamc@3
|
79 space,
|
adamc@3
|
80 string "->",
|
adamc@3
|
81 space,
|
adamc@3
|
82 p_con (E.pushCRel env x k) c])
|
adamc@345
|
83 | CDisjoint (_, c1, c2, c3) => parenIf par (box [p_con env c1,
|
adamc@334
|
84 space,
|
adamc@334
|
85 string "~",
|
adamc@334
|
86 space,
|
adamc@334
|
87 p_con env c2,
|
adamc@334
|
88 space,
|
adamc@345
|
89 string "=>",
|
adamc@334
|
90 space,
|
adamc@334
|
91 p_con env c3])
|
adamc@3
|
92 | TRecord (CRecord (_, xcs), _) => box [string "{",
|
adamc@3
|
93 p_list (fn (x, c) =>
|
adamc@20
|
94 box [p_name env x,
|
adamc@3
|
95 space,
|
adamc@3
|
96 string ":",
|
adamc@3
|
97 space,
|
adamc@3
|
98 p_con env c]) xcs,
|
adamc@3
|
99 string "}"]
|
adamc@3
|
100 | TRecord c => box [string "$",
|
adamc@3
|
101 p_con' true env c]
|
adamc@3
|
102
|
adamc@11
|
103 | CRel n =>
|
adamc@71
|
104 ((if !debug then
|
adamc@71
|
105 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@71
|
106 else
|
adamc@71
|
107 string (#1 (E.lookupCRel env n)))
|
adamc@71
|
108 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@11
|
109 | CNamed n =>
|
adamc@34
|
110 ((if !debug then
|
adamc@34
|
111 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@34
|
112 else
|
adamc@34
|
113 string (#1 (E.lookupCNamed env n)))
|
adamc@34
|
114 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@34
|
115 | CModProj (m1, ms, x) =>
|
adamc@34
|
116 let
|
adamc@88
|
117 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
118 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@88
|
119
|
adamc@34
|
120 val m1s = if !debug then
|
adamc@34
|
121 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
122 else
|
adamc@34
|
123 m1x
|
adamc@34
|
124 in
|
adamc@34
|
125 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@88
|
126 end
|
adamc@3
|
127
|
adamc@3
|
128 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@3
|
129 space,
|
adamc@3
|
130 p_con' true env c2])
|
adamc@147
|
131 | CAbs (x, k, c) => parenIf true (box [string "fn",
|
adamc@147
|
132 space,
|
adamc@147
|
133 string x,
|
adamc@147
|
134 space,
|
adamc@147
|
135 string "::",
|
adamc@147
|
136 space,
|
adamc@147
|
137 p_kind k,
|
adamc@147
|
138 space,
|
adamc@147
|
139 string "=>",
|
adamc@147
|
140 space,
|
adamc@147
|
141 p_con (E.pushCRel env x k) c])
|
adamc@3
|
142
|
adamc@3
|
143 | CName s => box [string "#", string s]
|
adamc@3
|
144
|
adamc@12
|
145 | CRecord (k, xcs) =>
|
adamc@12
|
146 if !debug then
|
adamc@12
|
147 parenIf par (box [string "[",
|
adamc@12
|
148 p_list (fn (x, c) =>
|
adamc@12
|
149 box [p_con env x,
|
adamc@12
|
150 space,
|
adamc@12
|
151 string "=",
|
adamc@12
|
152 space,
|
adamc@12
|
153 p_con env c]) xcs,
|
adamc@12
|
154 string "]::",
|
adamc@12
|
155 p_kind k])
|
adamc@12
|
156 else
|
adamc@12
|
157 parenIf par (box [string "[",
|
adamc@12
|
158 p_list (fn (x, c) =>
|
adamc@12
|
159 box [p_con env x,
|
adamc@12
|
160 space,
|
adamc@12
|
161 string "=",
|
adamc@12
|
162 space,
|
adamc@12
|
163 p_con env c]) xcs,
|
adamc@12
|
164 string "]"])
|
adamc@3
|
165 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@3
|
166 space,
|
adamc@3
|
167 string "++",
|
adamc@3
|
168 space,
|
adamc@3
|
169 p_con env c2])
|
adamc@67
|
170 | CFold _ => string "fold"
|
adamc@3
|
171
|
adamc@82
|
172 | CUnit => string "()"
|
adamc@82
|
173
|
adamc@207
|
174 | CTuple cs => box [string "(",
|
adamc@207
|
175 p_list (p_con env) cs,
|
adamc@207
|
176 string ")"]
|
adamc@207
|
177 | CProj (c, n) => box [p_con env c,
|
adamc@207
|
178 string ".",
|
adamc@207
|
179 string (Int.toString n)]
|
adamc@207
|
180
|
adamc@3
|
181 | CError => string "<ERROR>"
|
adamc@76
|
182 | CUnif (_, _, _, ref (SOME c)) => p_con' par env c
|
adamc@76
|
183 | CUnif (_, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
|
adamc@76
|
184 p_kind k,
|
adamc@76
|
185 string ">"]
|
adamc@3
|
186
|
adamc@3
|
187 and p_con env = p_con' false env
|
adamc@3
|
188
|
adamc@20
|
189 and p_name env (all as (c, _)) =
|
adamc@20
|
190 case c of
|
adamc@20
|
191 CName s => string s
|
adamc@20
|
192 | _ => p_con env all
|
adamc@20
|
193
|
adamc@171
|
194 fun p_patCon env pc =
|
adamc@171
|
195 case pc of
|
adamc@171
|
196 PConVar n =>
|
adamc@171
|
197 ((if !debug then
|
adamc@171
|
198 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@171
|
199 else
|
adamc@171
|
200 string (#1 (E.lookupENamed env n)))
|
adamc@448
|
201 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@171
|
202 | PConProj (m1, ms, x) =>
|
adamc@171
|
203 let
|
adamc@171
|
204 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@171
|
205 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@171
|
206
|
adamc@171
|
207 val m1s = if !debug then
|
adamc@171
|
208 m1x ^ "__" ^ Int.toString m1
|
adamc@171
|
209 else
|
adamc@171
|
210 m1x
|
adamc@171
|
211 in
|
adamc@171
|
212 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@171
|
213 end
|
adamc@171
|
214
|
adamc@171
|
215 fun p_pat' par env (p, _) =
|
adamc@171
|
216 case p of
|
adamc@171
|
217 PWild => string "_"
|
adamc@182
|
218 | PVar (s, _) => string s
|
adamc@173
|
219 | PPrim p => Prim.p_t p
|
adamc@191
|
220 | PCon (_, pc, _, NONE) => p_patCon env pc
|
adamc@191
|
221 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
|
adamc@188
|
222 space,
|
adamc@188
|
223 p_pat' true env p])
|
adamc@176
|
224 | PRecord xps =>
|
adamc@176
|
225 box [string "{",
|
adamc@182
|
226 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
|
adamc@176
|
227 box [string x,
|
adamc@176
|
228 space,
|
adamc@176
|
229 string "=",
|
adamc@176
|
230 space,
|
adamc@176
|
231 p_pat env p]) xps,
|
adamc@176
|
232 string "}"]
|
adamc@171
|
233
|
adamc@175
|
234 and p_pat x = p_pat' false x
|
adamc@171
|
235
|
adamc@9
|
236 fun p_exp' par env (e, _) =
|
adamc@9
|
237 case e of
|
adamc@14
|
238 EPrim p => Prim.p_t p
|
adamc@14
|
239 | ERel n =>
|
adamc@88
|
240 ((if !debug then
|
adamc@88
|
241 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@88
|
242 else
|
adamc@88
|
243 string (#1 (E.lookupERel env n)))
|
adamc@88
|
244 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@11
|
245 | ENamed n =>
|
adamc@88
|
246 ((if !debug then
|
adamc@88
|
247 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@88
|
248 else
|
adamc@88
|
249 string (#1 (E.lookupENamed env n)))
|
adamc@448
|
250 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@34
|
251 | EModProj (m1, ms, x) =>
|
adamc@34
|
252 let
|
adamc@88
|
253 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
254 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@88
|
255
|
adamc@34
|
256 val m1s = if !debug then
|
adamc@34
|
257 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
258 else
|
adamc@34
|
259 m1x
|
adamc@34
|
260 in
|
adamc@34
|
261 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@34
|
262 end
|
adamc@34
|
263
|
adamc@9
|
264 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@9
|
265 space,
|
adamc@9
|
266 p_exp' true env e2])
|
adamc@26
|
267 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@26
|
268 space,
|
adamc@26
|
269 string x,
|
adamc@26
|
270 space,
|
adamc@26
|
271 string ":",
|
adamc@26
|
272 space,
|
adamc@26
|
273 p_con env t,
|
adamc@26
|
274 space,
|
adamc@26
|
275 string "=>",
|
adamc@26
|
276 space,
|
adamc@26
|
277 p_exp (E.pushERel env x t) e])
|
adamc@9
|
278 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@9
|
279 space,
|
adamc@9
|
280 string "[",
|
adamc@9
|
281 p_con env c,
|
adamc@9
|
282 string "]"])
|
adamc@9
|
283 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@9
|
284 space,
|
adamc@9
|
285 string x,
|
adamc@9
|
286 space,
|
adamc@9
|
287 p_explicitness exp,
|
adamc@9
|
288 space,
|
adamc@9
|
289 p_kind k,
|
adamc@9
|
290 space,
|
adamc@9
|
291 string "=>",
|
adamc@9
|
292 space,
|
adamc@9
|
293 p_exp (E.pushCRel env x k) e])
|
adamc@9
|
294
|
adamc@12
|
295 | ERecord xes => box [string "{",
|
adamc@29
|
296 p_list (fn (x, e, _) =>
|
adamc@21
|
297 box [p_name env x,
|
adamc@12
|
298 space,
|
adamc@12
|
299 string "=",
|
adamc@12
|
300 space,
|
adamc@12
|
301 p_exp env e]) xes,
|
adamc@12
|
302 string "}"]
|
adamc@12
|
303 | EField (e, c, {field, rest}) =>
|
adamc@12
|
304 if !debug then
|
adamc@12
|
305 box [p_exp' true env e,
|
adamc@12
|
306 string ".",
|
adamc@12
|
307 p_con' true env c,
|
adamc@12
|
308 space,
|
adamc@12
|
309 string "[",
|
adamc@12
|
310 p_con env field,
|
adamc@12
|
311 space,
|
adamc@12
|
312 string " in ",
|
adamc@12
|
313 space,
|
adamc@12
|
314 p_con env rest,
|
adamc@12
|
315 string "]"]
|
adamc@12
|
316 else
|
adamc@12
|
317 box [p_exp' true env e,
|
adamc@12
|
318 string ".",
|
adamc@12
|
319 p_con' true env c]
|
adamc@445
|
320 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
321 parenIf par (if !debug then
|
adamc@445
|
322 box [p_exp' true env e1,
|
adamc@445
|
323 space,
|
adamc@445
|
324 string ":",
|
adamc@445
|
325 space,
|
adamc@445
|
326 p_con env c1,
|
adamc@445
|
327 space,
|
adamc@445
|
328 string "++",
|
adamc@445
|
329 space,
|
adamc@445
|
330 p_exp' true env e2,
|
adamc@445
|
331 space,
|
adamc@445
|
332 string ":",
|
adamc@445
|
333 space,
|
adamc@445
|
334 p_con env c2]
|
adamc@445
|
335 else
|
adamc@445
|
336 box [p_exp' true env e1,
|
adamc@339
|
337 space,
|
adamc@339
|
338 string "with",
|
adamc@339
|
339 space,
|
adamc@339
|
340 p_exp' true env e2])
|
adamc@149
|
341 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
342 parenIf par (if !debug then
|
adamc@149
|
343 box [p_exp' true env e,
|
adamc@149
|
344 space,
|
adamc@149
|
345 string "--",
|
adamc@149
|
346 space,
|
adamc@149
|
347 p_con' true env c,
|
adamc@149
|
348 space,
|
adamc@149
|
349 string "[",
|
adamc@149
|
350 p_con env field,
|
adamc@149
|
351 space,
|
adamc@149
|
352 string " in ",
|
adamc@149
|
353 space,
|
adamc@149
|
354 p_con env rest,
|
adamc@149
|
355 string "]"]
|
adamc@149
|
356 else
|
adamc@149
|
357 box [p_exp' true env e,
|
adamc@149
|
358 space,
|
adamc@149
|
359 string "--",
|
adamc@149
|
360 space,
|
adamc@149
|
361 p_con' true env c])
|
adamc@72
|
362 | EFold _ => string "fold"
|
adamc@71
|
363
|
adamc@171
|
364 | ECase (e, pes, _) => parenIf par (box [string "case",
|
adamc@171
|
365 space,
|
adamc@171
|
366 p_exp env e,
|
adamc@171
|
367 space,
|
adamc@171
|
368 string "of",
|
adamc@171
|
369 space,
|
adamc@171
|
370 p_list_sep (box [space, string "|", space])
|
adamc@171
|
371 (fn (p, e) => box [p_pat env p,
|
adamc@171
|
372 space,
|
adamc@171
|
373 string "=>",
|
adamc@171
|
374 space,
|
adamc@243
|
375 p_exp (E.patBinds env p) e]) pes])
|
adamc@171
|
376
|
adamc@9
|
377 | EError => string "<ERROR>"
|
adamc@228
|
378 | EUnif (ref (SOME e)) => p_exp env e
|
adamc@228
|
379 | EUnif _ => string "_"
|
adamc@9
|
380
|
adamc@447
|
381 | ELet (ds, e) =>
|
adamc@447
|
382 let
|
adamc@447
|
383 val (dsp, env) = ListUtil.foldlMap
|
adamc@447
|
384 (fn (d, env) =>
|
adamc@447
|
385 (p_edecl env d,
|
adamc@447
|
386 E.edeclBinds env d))
|
adamc@447
|
387 env ds
|
adamc@447
|
388 in
|
adamc@447
|
389 box [string "let",
|
adamc@447
|
390 newline,
|
adamc@447
|
391 box [p_list_sep newline (fn x => x) dsp],
|
adamc@447
|
392 newline,
|
adamc@447
|
393 string "in",
|
adamc@447
|
394 newline,
|
adamc@447
|
395 box [p_exp env e],
|
adamc@447
|
396 newline,
|
adamc@447
|
397 string "end"]
|
adamc@447
|
398 end
|
adamc@447
|
399
|
adamc@9
|
400 and p_exp env = p_exp' false env
|
adamc@9
|
401
|
adamc@447
|
402 and p_edecl env (dAll as (d, _)) =
|
adamc@447
|
403 case d of
|
adamc@447
|
404 EDVal vi => box [string "val",
|
adamc@447
|
405 space,
|
adamc@447
|
406 p_evali env vi]
|
adamc@447
|
407 | EDValRec vis =>
|
adamc@447
|
408 let
|
adamc@447
|
409 val env = E.edeclBinds env dAll
|
adamc@447
|
410 in
|
adamc@447
|
411 box [string "val",
|
adamc@447
|
412 space,
|
adamc@447
|
413 string "rec",
|
adamc@447
|
414 space,
|
adamc@447
|
415 p_list_sep (box [newline, string "and", space]) (p_evali env) vis]
|
adamc@447
|
416 end
|
adamc@447
|
417
|
adamc@447
|
418 and p_evali env (x, t, e) = box [string x,
|
adamc@447
|
419 space,
|
adamc@447
|
420 string ":",
|
adamc@447
|
421 space,
|
adamc@447
|
422 p_con env t,
|
adamc@447
|
423 space,
|
adamc@447
|
424 string "=",
|
adamc@447
|
425 space,
|
adamc@447
|
426 p_exp env e]
|
adamc@31
|
427
|
adamc@191
|
428 fun p_datatype env (x, n, xs, cons) =
|
adamc@156
|
429 let
|
adamc@191
|
430 val k = (KType, ErrorMsg.dummySpan)
|
adamc@191
|
431 val env = E.pushCNamedAs env x n k NONE
|
adamc@191
|
432 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
|
adamc@156
|
433 in
|
adamc@156
|
434 box [string "datatype",
|
adamc@156
|
435 space,
|
adamc@156
|
436 string x,
|
adamc@191
|
437 p_list_sep (box []) (fn x => box [space, string x]) xs,
|
adamc@156
|
438 space,
|
adamc@156
|
439 string "=",
|
adamc@156
|
440 space,
|
adamc@156
|
441 p_list_sep (box [space, string "|", space])
|
adamc@156
|
442 (fn (x, _, NONE) => string x
|
adamc@156
|
443 | (x, _, SOME t) => box [string x, space, string "of", space, p_con env t])
|
adamc@156
|
444 cons]
|
adamc@156
|
445 end
|
adamc@156
|
446
|
adamc@447
|
447 fun p_named x n =
|
adamc@447
|
448 if !debug then
|
adamc@447
|
449 box [string x,
|
adamc@447
|
450 string "__",
|
adamc@447
|
451 string (Int.toString n)]
|
adamc@447
|
452 else
|
adamc@447
|
453 string x
|
adamc@447
|
454
|
adamc@31
|
455 fun p_sgn_item env (sgi, _) =
|
adamc@31
|
456 case sgi of
|
adamc@31
|
457 SgiConAbs (x, n, k) => box [string "con",
|
adamc@31
|
458 space,
|
adamc@31
|
459 p_named x n,
|
adamc@31
|
460 space,
|
adamc@31
|
461 string "::",
|
adamc@31
|
462 space,
|
adamc@31
|
463 p_kind k]
|
adamc@31
|
464 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@31
|
465 space,
|
adamc@31
|
466 p_named x n,
|
adamc@31
|
467 space,
|
adamc@31
|
468 string "::",
|
adamc@31
|
469 space,
|
adamc@31
|
470 p_kind k,
|
adamc@31
|
471 space,
|
adamc@31
|
472 string "=",
|
adamc@31
|
473 space,
|
adamc@31
|
474 p_con env c]
|
adamc@156
|
475 | SgiDatatype x => p_datatype env x
|
adamc@191
|
476 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@156
|
477 let
|
adamc@156
|
478 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@156
|
479 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@156
|
480 in
|
adamc@156
|
481 box [string "datatype",
|
adamc@156
|
482 space,
|
adamc@156
|
483 string x,
|
adamc@156
|
484 space,
|
adamc@156
|
485 string "=",
|
adamc@156
|
486 space,
|
adamc@156
|
487 string "datatype",
|
adamc@156
|
488 space,
|
adamc@156
|
489 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@156
|
490 end
|
adamc@31
|
491 | SgiVal (x, n, c) => box [string "val",
|
adamc@31
|
492 space,
|
adamc@31
|
493 p_named x n,
|
adamc@31
|
494 space,
|
adamc@31
|
495 string ":",
|
adamc@31
|
496 space,
|
adamc@31
|
497 p_con env c]
|
adamc@31
|
498 | SgiStr (x, n, sgn) => box [string "structure",
|
adamc@31
|
499 space,
|
adamc@31
|
500 p_named x n,
|
adamc@31
|
501 space,
|
adamc@31
|
502 string ":",
|
adamc@31
|
503 space,
|
adamc@31
|
504 p_sgn env sgn]
|
adamc@59
|
505 | SgiSgn (x, n, sgn) => box [string "signature",
|
adamc@59
|
506 space,
|
adamc@59
|
507 p_named x n,
|
adamc@59
|
508 space,
|
adamc@59
|
509 string "=",
|
adamc@59
|
510 space,
|
adamc@59
|
511 p_sgn env sgn]
|
adamc@88
|
512 | SgiConstraint (c1, c2) => box [string "constraint",
|
adamc@88
|
513 space,
|
adamc@88
|
514 p_con env c1,
|
adamc@88
|
515 space,
|
adamc@88
|
516 string "~",
|
adamc@88
|
517 space,
|
adamc@88
|
518 p_con env c2]
|
adamc@205
|
519 | SgiTable (_, x, n, c) => box [string "table",
|
adamc@205
|
520 space,
|
adamc@205
|
521 p_named x n,
|
adamc@205
|
522 space,
|
adamc@205
|
523 string ":",
|
adamc@205
|
524 space,
|
adamc@205
|
525 p_con env c]
|
adamc@338
|
526 | SgiSequence (_, x, n) => box [string "sequence",
|
adamc@338
|
527 space,
|
adamc@338
|
528 p_named x n]
|
adamc@211
|
529 | SgiClassAbs (x, n) => box [string "class",
|
adamc@211
|
530 space,
|
adamc@211
|
531 p_named x n]
|
adamc@211
|
532 | SgiClass (x, n, c) => box [string "class",
|
adamc@211
|
533 space,
|
adamc@211
|
534 p_named x n,
|
adamc@211
|
535 space,
|
adamc@211
|
536 string "=",
|
adamc@211
|
537 space,
|
adamc@211
|
538 p_con env c]
|
adamc@459
|
539 | SgiCookie (_, x, n, c) => box [string "cookie",
|
adamc@459
|
540 space,
|
adamc@459
|
541 p_named x n,
|
adamc@459
|
542 space,
|
adamc@459
|
543 string ":",
|
adamc@459
|
544 space,
|
adamc@459
|
545 p_con env c]
|
adamc@31
|
546
|
adamc@31
|
547 and p_sgn env (sgn, _) =
|
adamc@31
|
548 case sgn of
|
adamc@31
|
549 SgnConst sgis => box [string "sig",
|
adamc@31
|
550 newline,
|
adamc@32
|
551 let
|
adamc@32
|
552 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
|
adamc@32
|
553 (p_sgn_item env sgi,
|
adamc@32
|
554 E.sgiBinds env sgi))
|
adamc@32
|
555 env sgis
|
adamc@32
|
556 in
|
adamc@32
|
557 p_list_sep newline (fn x => x) psgis
|
adamc@32
|
558 end,
|
adamc@31
|
559 newline,
|
adamc@31
|
560 string "end"]
|
adamc@88
|
561 | SgnVar n => ((string (#1 (E.lookupSgnNamed env n)))
|
adamc@88
|
562 handle E.UnboundNamed _ => string ("UNBOUND_SGN_" ^ Int.toString n))
|
adamc@41
|
563 | SgnFun (x, n, sgn, sgn') => box [string "functor",
|
adamc@41
|
564 space,
|
adamc@41
|
565 string "(",
|
adamc@41
|
566 string x,
|
adamc@41
|
567 space,
|
adamc@41
|
568 string ":",
|
adamc@41
|
569 space,
|
adamc@41
|
570 p_sgn env sgn,
|
adamc@41
|
571 string ")",
|
adamc@41
|
572 space,
|
adamc@41
|
573 string ":",
|
adamc@41
|
574 space,
|
adamc@41
|
575 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
|
adamc@42
|
576 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
|
adamc@42
|
577 space,
|
adamc@42
|
578 string "where",
|
adamc@42
|
579 space,
|
adamc@42
|
580 string "con",
|
adamc@42
|
581 space,
|
adamc@42
|
582 string x,
|
adamc@42
|
583 space,
|
adamc@42
|
584 string "=",
|
adamc@42
|
585 space,
|
adamc@42
|
586 p_con env c]
|
adamc@59
|
587 | SgnProj (m1, ms, x) =>
|
adamc@59
|
588 let
|
adamc@88
|
589 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
590 handle E.UnboundNamed _ => "UNBOUND_SGN_" ^ Int.toString m1
|
adamc@88
|
591
|
adamc@59
|
592 val m1s = if !debug then
|
adamc@59
|
593 m1x ^ "__" ^ Int.toString m1
|
adamc@59
|
594 else
|
adamc@59
|
595 m1x
|
adamc@88
|
596 in
|
adamc@59
|
597 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@59
|
598 end
|
adamc@31
|
599 | SgnError => string "<ERROR>"
|
adamc@31
|
600
|
adamc@123
|
601 fun p_vali env (x, n, t, e) = box [p_named x n,
|
adamc@123
|
602 space,
|
adamc@123
|
603 string ":",
|
adamc@123
|
604 space,
|
adamc@123
|
605 p_con env t,
|
adamc@123
|
606 space,
|
adamc@123
|
607 string "=",
|
adamc@123
|
608 space,
|
adamc@123
|
609 p_exp env e]
|
adamc@123
|
610
|
adamc@447
|
611
|
adamc@447
|
612
|
adamc@123
|
613 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@3
|
614 case d of
|
adamc@31
|
615 DCon (x, n, k, c) => box [string "con",
|
adamc@31
|
616 space,
|
adamc@31
|
617 p_named x n,
|
adamc@31
|
618 space,
|
adamc@31
|
619 string "::",
|
adamc@31
|
620 space,
|
adamc@31
|
621 p_kind k,
|
adamc@31
|
622 space,
|
adamc@31
|
623 string "=",
|
adamc@31
|
624 space,
|
adamc@31
|
625 p_con env c]
|
adamc@156
|
626 | DDatatype x => p_datatype env x
|
adamc@191
|
627 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@156
|
628 let
|
adamc@156
|
629 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@156
|
630 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@156
|
631 in
|
adamc@156
|
632 box [string "datatype",
|
adamc@156
|
633 space,
|
adamc@156
|
634 string x,
|
adamc@156
|
635 space,
|
adamc@156
|
636 string "=",
|
adamc@156
|
637 space,
|
adamc@156
|
638 string "datatype",
|
adamc@156
|
639 space,
|
adamc@156
|
640 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@156
|
641 end
|
adamc@123
|
642 | DVal vi => box [string "val",
|
adamc@123
|
643 space,
|
adamc@123
|
644 p_vali env vi]
|
adamc@123
|
645 | DValRec vis =>
|
adamc@123
|
646 let
|
adamc@123
|
647 val env = E.declBinds env dAll
|
adamc@123
|
648 in
|
adamc@123
|
649 box [string "val",
|
adamc@123
|
650 space,
|
adamc@123
|
651 string "rec",
|
adamc@123
|
652 space,
|
adamc@123
|
653 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
|
adamc@123
|
654 end
|
adamc@31
|
655
|
adamc@31
|
656 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@31
|
657 space,
|
adamc@31
|
658 p_named x n,
|
adamc@31
|
659 space,
|
adamc@31
|
660 string "=",
|
adamc@31
|
661 space,
|
adamc@31
|
662 p_sgn env sgn]
|
adamc@31
|
663 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@31
|
664 space,
|
adamc@31
|
665 p_named x n,
|
adamc@31
|
666 space,
|
adamc@31
|
667 string ":",
|
adamc@31
|
668 space,
|
adamc@31
|
669 p_sgn env sgn,
|
adamc@31
|
670 space,
|
adamc@31
|
671 string "=",
|
adamc@31
|
672 space,
|
adamc@31
|
673 p_str env str]
|
adamc@48
|
674 | DFfiStr (x, n, sgn) => box [string "extern",
|
adamc@48
|
675 space,
|
adamc@48
|
676 string "structure",
|
adamc@48
|
677 space,
|
adamc@48
|
678 p_named x n,
|
adamc@48
|
679 space,
|
adamc@48
|
680 string ":",
|
adamc@48
|
681 space,
|
adamc@48
|
682 p_sgn env sgn]
|
adamc@88
|
683 | DConstraint (c1, c2) => box [string "constraint",
|
adamc@88
|
684 space,
|
adamc@88
|
685 p_con env c1,
|
adamc@88
|
686 space,
|
adamc@88
|
687 string "~",
|
adamc@88
|
688 space,
|
adamc@88
|
689 p_con env c2]
|
adamc@109
|
690 | DExport (_, sgn, str) => box [string "export",
|
adamc@110
|
691 space,
|
adamc@109
|
692 p_str env str,
|
adamc@109
|
693 space,
|
adamc@109
|
694 string ":",
|
adamc@109
|
695 space,
|
adamc@109
|
696 p_sgn env sgn]
|
adamc@205
|
697 | DTable (_, x, n, c) => box [string "table",
|
adamc@205
|
698 space,
|
adamc@205
|
699 p_named x n,
|
adamc@205
|
700 space,
|
adamc@205
|
701 string ":",
|
adamc@205
|
702 space,
|
adamc@205
|
703 p_con env c]
|
adamc@338
|
704 | DSequence (_, x, n) => box [string "sequence",
|
adamc@338
|
705 space,
|
adamc@338
|
706 p_named x n]
|
adamc@271
|
707 | DClass (x, n, c) => box [string "class",
|
adamc@271
|
708 space,
|
adamc@271
|
709 p_named x n,
|
adamc@271
|
710 space,
|
adamc@271
|
711 string "=",
|
adamc@271
|
712 space,
|
adamc@271
|
713 p_con env c]
|
adamc@271
|
714 | DDatabase s => box [string "database",
|
adamc@271
|
715 space,
|
adamc@271
|
716 string s]
|
adamc@459
|
717 | DCookie (_, x, n, c) => box [string "cookie",
|
adamc@459
|
718 space,
|
adamc@459
|
719 p_named x n,
|
adamc@459
|
720 space,
|
adamc@459
|
721 string ":",
|
adamc@459
|
722 space,
|
adamc@459
|
723 p_con env c]
|
adamc@31
|
724
|
adamc@31
|
725 and p_str env (str, _) =
|
adamc@31
|
726 case str of
|
adamc@31
|
727 StrConst ds => box [string "struct",
|
adamc@31
|
728 newline,
|
adamc@32
|
729 p_file env ds,
|
adamc@31
|
730 newline,
|
adamc@31
|
731 string "end"]
|
adamc@88
|
732 | StrVar n => ((string (#1 (E.lookupStrNamed env n)))
|
adamc@88
|
733 handle E.UnboundNamed _ => string ("UNBOUND_STR_" ^ Int.toString n))
|
adamc@34
|
734 | StrProj (str, s) => box [p_str env str,
|
adamc@34
|
735 string ".",
|
adamc@34
|
736 string s]
|
adamc@41
|
737 | StrFun (x, n, sgn, sgn', str) =>
|
adamc@41
|
738 let
|
adamc@41
|
739 val env' = E.pushStrNamedAs env x n sgn
|
adamc@41
|
740 in
|
adamc@41
|
741 box [string "functor",
|
adamc@41
|
742 space,
|
adamc@41
|
743 string "(",
|
adamc@41
|
744 string x,
|
adamc@41
|
745 space,
|
adamc@41
|
746 string ":",
|
adamc@41
|
747 space,
|
adamc@41
|
748 p_sgn env sgn,
|
adamc@41
|
749 string ")",
|
adamc@41
|
750 space,
|
adamc@41
|
751 string ":",
|
adamc@41
|
752 space,
|
adamc@41
|
753 p_sgn env' sgn',
|
adamc@41
|
754 space,
|
adamc@41
|
755 string "=>",
|
adamc@41
|
756 space,
|
adamc@41
|
757 p_str env' str]
|
adamc@41
|
758 end
|
adamc@44
|
759 | StrApp (str1, str2) => box [p_str env str1,
|
adamc@44
|
760 string "(",
|
adamc@44
|
761 p_str env str2,
|
adamc@44
|
762 string ")"]
|
adamc@31
|
763 | StrError => string "<ERROR>"
|
adamc@3
|
764
|
adamc@32
|
765 and p_file env file =
|
adamc@3
|
766 let
|
adamc@31
|
767 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
768 (p_decl env d,
|
adamc@31
|
769 E.declBinds env d))
|
adamc@31
|
770 env file
|
adamc@3
|
771 in
|
adamc@3
|
772 p_list_sep newline (fn x => x) pds
|
adamc@3
|
773 end
|
adamc@3
|
774
|
adamc@3
|
775 end
|