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@3
|
28 (* Pretty-printing elaborated Laconic/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@3
|
52
|
adamc@3
|
53 | KError => string "<ERROR>"
|
adamc@76
|
54 | KUnif (_, _, ref (SOME k)) => p_kind' par k
|
adamc@76
|
55 | KUnif (_, s, _) => string ("<UNIF:" ^ s ^ ">")
|
adamc@3
|
56
|
adamc@3
|
57 and p_kind k = p_kind' false k
|
adamc@3
|
58
|
adamc@3
|
59 fun p_explicitness e =
|
adamc@3
|
60 case e of
|
adamc@3
|
61 Explicit => string "::"
|
adamc@3
|
62 | Implicit => string ":::"
|
adamc@3
|
63
|
adamc@3
|
64 fun p_con' par env (c, _) =
|
adamc@3
|
65 case c of
|
adamc@3
|
66 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
|
adamc@3
|
67 space,
|
adamc@3
|
68 string "->",
|
adamc@3
|
69 space,
|
adamc@3
|
70 p_con env t2])
|
adamc@3
|
71 | TCFun (e, x, k, c) => parenIf par (box [string x,
|
adamc@3
|
72 space,
|
adamc@3
|
73 p_explicitness e,
|
adamc@3
|
74 space,
|
adamc@3
|
75 p_kind k,
|
adamc@3
|
76 space,
|
adamc@3
|
77 string "->",
|
adamc@3
|
78 space,
|
adamc@3
|
79 p_con (E.pushCRel env x k) c])
|
adamc@3
|
80 | TRecord (CRecord (_, xcs), _) => box [string "{",
|
adamc@3
|
81 p_list (fn (x, c) =>
|
adamc@20
|
82 box [p_name env x,
|
adamc@3
|
83 space,
|
adamc@3
|
84 string ":",
|
adamc@3
|
85 space,
|
adamc@3
|
86 p_con env c]) xcs,
|
adamc@3
|
87 string "}"]
|
adamc@3
|
88 | TRecord c => box [string "$",
|
adamc@3
|
89 p_con' true env c]
|
adamc@3
|
90
|
adamc@11
|
91 | CRel n =>
|
adamc@71
|
92 ((if !debug then
|
adamc@71
|
93 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@71
|
94 else
|
adamc@71
|
95 string (#1 (E.lookupCRel env n)))
|
adamc@71
|
96 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@11
|
97 | CNamed n =>
|
adamc@34
|
98 ((if !debug then
|
adamc@34
|
99 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@34
|
100 else
|
adamc@34
|
101 string (#1 (E.lookupCNamed env n)))
|
adamc@34
|
102 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@34
|
103 | CModProj (m1, ms, x) =>
|
adamc@34
|
104 let
|
adamc@34
|
105 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@34
|
106
|
adamc@34
|
107 val m1s = if !debug then
|
adamc@34
|
108 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
109 else
|
adamc@34
|
110 m1x
|
adamc@34
|
111 in
|
adamc@34
|
112 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@34
|
113 end
|
adamc@3
|
114
|
adamc@3
|
115 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@3
|
116 space,
|
adamc@3
|
117 p_con' true env c2])
|
adamc@8
|
118 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@8
|
119 space,
|
adamc@8
|
120 string x,
|
adamc@8
|
121 space,
|
adamc@8
|
122 string "::",
|
adamc@8
|
123 space,
|
adamc@8
|
124 p_kind k,
|
adamc@8
|
125 space,
|
adamc@8
|
126 string "=>",
|
adamc@8
|
127 space,
|
adamc@8
|
128 p_con (E.pushCRel env x k) c])
|
adamc@3
|
129
|
adamc@3
|
130 | CName s => box [string "#", string s]
|
adamc@3
|
131
|
adamc@12
|
132 | CRecord (k, xcs) =>
|
adamc@12
|
133 if !debug then
|
adamc@12
|
134 parenIf par (box [string "[",
|
adamc@12
|
135 p_list (fn (x, c) =>
|
adamc@12
|
136 box [p_con env x,
|
adamc@12
|
137 space,
|
adamc@12
|
138 string "=",
|
adamc@12
|
139 space,
|
adamc@12
|
140 p_con env c]) xcs,
|
adamc@12
|
141 string "]::",
|
adamc@12
|
142 p_kind k])
|
adamc@12
|
143 else
|
adamc@12
|
144 parenIf par (box [string "[",
|
adamc@12
|
145 p_list (fn (x, c) =>
|
adamc@12
|
146 box [p_con env x,
|
adamc@12
|
147 space,
|
adamc@12
|
148 string "=",
|
adamc@12
|
149 space,
|
adamc@12
|
150 p_con env c]) xcs,
|
adamc@12
|
151 string "]"])
|
adamc@3
|
152 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@3
|
153 space,
|
adamc@3
|
154 string "++",
|
adamc@3
|
155 space,
|
adamc@3
|
156 p_con env c2])
|
adamc@67
|
157 | CFold _ => string "fold"
|
adamc@3
|
158
|
adamc@82
|
159 | CUnit => string "()"
|
adamc@82
|
160
|
adamc@3
|
161 | CError => string "<ERROR>"
|
adamc@76
|
162 | CUnif (_, _, _, ref (SOME c)) => p_con' par env c
|
adamc@76
|
163 | CUnif (_, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
|
adamc@76
|
164 p_kind k,
|
adamc@76
|
165 string ">"]
|
adamc@3
|
166
|
adamc@3
|
167 and p_con env = p_con' false env
|
adamc@3
|
168
|
adamc@20
|
169 and p_name env (all as (c, _)) =
|
adamc@20
|
170 case c of
|
adamc@20
|
171 CName s => string s
|
adamc@20
|
172 | _ => p_con env all
|
adamc@20
|
173
|
adamc@9
|
174 fun p_exp' par env (e, _) =
|
adamc@9
|
175 case e of
|
adamc@14
|
176 EPrim p => Prim.p_t p
|
adamc@14
|
177 | ERel n =>
|
adamc@11
|
178 if !debug then
|
adamc@11
|
179 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@11
|
180 else
|
adamc@11
|
181 string (#1 (E.lookupERel env n))
|
adamc@11
|
182 | ENamed n =>
|
adamc@11
|
183 if !debug then
|
adamc@11
|
184 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@11
|
185 else
|
adamc@11
|
186 string (#1 (E.lookupENamed env n))
|
adamc@34
|
187 | EModProj (m1, ms, x) =>
|
adamc@34
|
188 let
|
adamc@34
|
189 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@34
|
190
|
adamc@34
|
191 val m1s = if !debug then
|
adamc@34
|
192 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
193 else
|
adamc@34
|
194 m1x
|
adamc@34
|
195 in
|
adamc@34
|
196 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@34
|
197 end
|
adamc@34
|
198
|
adamc@9
|
199 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@9
|
200 space,
|
adamc@9
|
201 p_exp' true env e2])
|
adamc@26
|
202 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@26
|
203 space,
|
adamc@26
|
204 string x,
|
adamc@26
|
205 space,
|
adamc@26
|
206 string ":",
|
adamc@26
|
207 space,
|
adamc@26
|
208 p_con env t,
|
adamc@26
|
209 space,
|
adamc@26
|
210 string "=>",
|
adamc@26
|
211 space,
|
adamc@26
|
212 p_exp (E.pushERel env x t) e])
|
adamc@9
|
213 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@9
|
214 space,
|
adamc@9
|
215 string "[",
|
adamc@9
|
216 p_con env c,
|
adamc@9
|
217 string "]"])
|
adamc@9
|
218 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@9
|
219 space,
|
adamc@9
|
220 string x,
|
adamc@9
|
221 space,
|
adamc@9
|
222 p_explicitness exp,
|
adamc@9
|
223 space,
|
adamc@9
|
224 p_kind k,
|
adamc@9
|
225 space,
|
adamc@9
|
226 string "=>",
|
adamc@9
|
227 space,
|
adamc@9
|
228 p_exp (E.pushCRel env x k) e])
|
adamc@9
|
229
|
adamc@12
|
230 | ERecord xes => box [string "{",
|
adamc@29
|
231 p_list (fn (x, e, _) =>
|
adamc@21
|
232 box [p_name env x,
|
adamc@12
|
233 space,
|
adamc@12
|
234 string "=",
|
adamc@12
|
235 space,
|
adamc@12
|
236 p_exp env e]) xes,
|
adamc@12
|
237 string "}"]
|
adamc@12
|
238 | EField (e, c, {field, rest}) =>
|
adamc@12
|
239 if !debug then
|
adamc@12
|
240 box [p_exp' true env e,
|
adamc@12
|
241 string ".",
|
adamc@12
|
242 p_con' true env c,
|
adamc@12
|
243 space,
|
adamc@12
|
244 string "[",
|
adamc@12
|
245 p_con env field,
|
adamc@12
|
246 space,
|
adamc@12
|
247 string " in ",
|
adamc@12
|
248 space,
|
adamc@12
|
249 p_con env rest,
|
adamc@12
|
250 string "]"]
|
adamc@12
|
251 else
|
adamc@12
|
252 box [p_exp' true env e,
|
adamc@12
|
253 string ".",
|
adamc@12
|
254 p_con' true env c]
|
adamc@72
|
255 | EFold _ => string "fold"
|
adamc@71
|
256
|
adamc@9
|
257 | EError => string "<ERROR>"
|
adamc@9
|
258
|
adamc@9
|
259 and p_exp env = p_exp' false env
|
adamc@9
|
260
|
adamc@31
|
261 fun p_named x n =
|
adamc@31
|
262 if !debug then
|
adamc@31
|
263 box [string x,
|
adamc@31
|
264 string "__",
|
adamc@31
|
265 string (Int.toString n)]
|
adamc@31
|
266 else
|
adamc@31
|
267 string x
|
adamc@31
|
268
|
adamc@31
|
269 fun p_sgn_item env (sgi, _) =
|
adamc@31
|
270 case sgi of
|
adamc@31
|
271 SgiConAbs (x, n, k) => box [string "con",
|
adamc@31
|
272 space,
|
adamc@31
|
273 p_named x n,
|
adamc@31
|
274 space,
|
adamc@31
|
275 string "::",
|
adamc@31
|
276 space,
|
adamc@31
|
277 p_kind k]
|
adamc@31
|
278 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@31
|
279 space,
|
adamc@31
|
280 p_named x n,
|
adamc@31
|
281 space,
|
adamc@31
|
282 string "::",
|
adamc@31
|
283 space,
|
adamc@31
|
284 p_kind k,
|
adamc@31
|
285 space,
|
adamc@31
|
286 string "=",
|
adamc@31
|
287 space,
|
adamc@31
|
288 p_con env c]
|
adamc@31
|
289 | SgiVal (x, n, c) => box [string "val",
|
adamc@31
|
290 space,
|
adamc@31
|
291 p_named x n,
|
adamc@31
|
292 space,
|
adamc@31
|
293 string ":",
|
adamc@31
|
294 space,
|
adamc@31
|
295 p_con env c]
|
adamc@31
|
296 | SgiStr (x, n, sgn) => box [string "structure",
|
adamc@31
|
297 space,
|
adamc@31
|
298 p_named x n,
|
adamc@31
|
299 space,
|
adamc@31
|
300 string ":",
|
adamc@31
|
301 space,
|
adamc@31
|
302 p_sgn env sgn]
|
adamc@59
|
303 | SgiSgn (x, n, sgn) => box [string "signature",
|
adamc@59
|
304 space,
|
adamc@59
|
305 p_named x n,
|
adamc@59
|
306 space,
|
adamc@59
|
307 string "=",
|
adamc@59
|
308 space,
|
adamc@59
|
309 p_sgn env sgn]
|
adamc@31
|
310
|
adamc@31
|
311 and p_sgn env (sgn, _) =
|
adamc@31
|
312 case sgn of
|
adamc@31
|
313 SgnConst sgis => box [string "sig",
|
adamc@31
|
314 newline,
|
adamc@32
|
315 let
|
adamc@32
|
316 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
|
adamc@32
|
317 (p_sgn_item env sgi,
|
adamc@32
|
318 E.sgiBinds env sgi))
|
adamc@32
|
319 env sgis
|
adamc@32
|
320 in
|
adamc@32
|
321 p_list_sep newline (fn x => x) psgis
|
adamc@32
|
322 end,
|
adamc@31
|
323 newline,
|
adamc@31
|
324 string "end"]
|
adamc@31
|
325 | SgnVar n => string (#1 (E.lookupSgnNamed env n))
|
adamc@41
|
326 | SgnFun (x, n, sgn, sgn') => box [string "functor",
|
adamc@41
|
327 space,
|
adamc@41
|
328 string "(",
|
adamc@41
|
329 string x,
|
adamc@41
|
330 space,
|
adamc@41
|
331 string ":",
|
adamc@41
|
332 space,
|
adamc@41
|
333 p_sgn env sgn,
|
adamc@41
|
334 string ")",
|
adamc@41
|
335 space,
|
adamc@41
|
336 string ":",
|
adamc@41
|
337 space,
|
adamc@41
|
338 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
|
adamc@42
|
339 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
|
adamc@42
|
340 space,
|
adamc@42
|
341 string "where",
|
adamc@42
|
342 space,
|
adamc@42
|
343 string "con",
|
adamc@42
|
344 space,
|
adamc@42
|
345 string x,
|
adamc@42
|
346 space,
|
adamc@42
|
347 string "=",
|
adamc@42
|
348 space,
|
adamc@42
|
349 p_con env c]
|
adamc@59
|
350 | SgnProj (m1, ms, x) =>
|
adamc@59
|
351 let
|
adamc@59
|
352 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@59
|
353
|
adamc@59
|
354 val m1s = if !debug then
|
adamc@59
|
355 m1x ^ "__" ^ Int.toString m1
|
adamc@59
|
356 else
|
adamc@59
|
357 m1x
|
adamc@59
|
358 in
|
adamc@59
|
359 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@59
|
360 end
|
adamc@31
|
361 | SgnError => string "<ERROR>"
|
adamc@31
|
362
|
adamc@3
|
363 fun p_decl env ((d, _) : decl) =
|
adamc@3
|
364 case d of
|
adamc@31
|
365 DCon (x, n, k, c) => box [string "con",
|
adamc@31
|
366 space,
|
adamc@31
|
367 p_named x n,
|
adamc@31
|
368 space,
|
adamc@31
|
369 string "::",
|
adamc@31
|
370 space,
|
adamc@31
|
371 p_kind k,
|
adamc@31
|
372 space,
|
adamc@31
|
373 string "=",
|
adamc@31
|
374 space,
|
adamc@31
|
375 p_con env c]
|
adamc@31
|
376 | DVal (x, n, t, e) => box [string "val",
|
adamc@31
|
377 space,
|
adamc@31
|
378 p_named x n,
|
adamc@31
|
379 space,
|
adamc@31
|
380 string ":",
|
adamc@31
|
381 space,
|
adamc@31
|
382 p_con env t,
|
adamc@31
|
383 space,
|
adamc@31
|
384 string "=",
|
adamc@31
|
385 space,
|
adamc@31
|
386 p_exp env e]
|
adamc@31
|
387
|
adamc@31
|
388 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@31
|
389 space,
|
adamc@31
|
390 p_named x n,
|
adamc@31
|
391 space,
|
adamc@31
|
392 string "=",
|
adamc@31
|
393 space,
|
adamc@31
|
394 p_sgn env sgn]
|
adamc@31
|
395 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@31
|
396 space,
|
adamc@31
|
397 p_named x n,
|
adamc@31
|
398 space,
|
adamc@31
|
399 string ":",
|
adamc@31
|
400 space,
|
adamc@31
|
401 p_sgn env sgn,
|
adamc@31
|
402 space,
|
adamc@31
|
403 string "=",
|
adamc@31
|
404 space,
|
adamc@31
|
405 p_str env str]
|
adamc@48
|
406 | DFfiStr (x, n, sgn) => box [string "extern",
|
adamc@48
|
407 space,
|
adamc@48
|
408 string "structure",
|
adamc@48
|
409 space,
|
adamc@48
|
410 p_named x n,
|
adamc@48
|
411 space,
|
adamc@48
|
412 string ":",
|
adamc@48
|
413 space,
|
adamc@48
|
414 p_sgn env sgn]
|
adamc@31
|
415
|
adamc@31
|
416 and p_str env (str, _) =
|
adamc@31
|
417 case str of
|
adamc@31
|
418 StrConst ds => box [string "struct",
|
adamc@31
|
419 newline,
|
adamc@32
|
420 p_file env ds,
|
adamc@31
|
421 newline,
|
adamc@31
|
422 string "end"]
|
adamc@31
|
423 | StrVar n => string (#1 (E.lookupStrNamed env n))
|
adamc@34
|
424 | StrProj (str, s) => box [p_str env str,
|
adamc@34
|
425 string ".",
|
adamc@34
|
426 string s]
|
adamc@41
|
427 | StrFun (x, n, sgn, sgn', str) =>
|
adamc@41
|
428 let
|
adamc@41
|
429 val env' = E.pushStrNamedAs env x n sgn
|
adamc@41
|
430 in
|
adamc@41
|
431 box [string "functor",
|
adamc@41
|
432 space,
|
adamc@41
|
433 string "(",
|
adamc@41
|
434 string x,
|
adamc@41
|
435 space,
|
adamc@41
|
436 string ":",
|
adamc@41
|
437 space,
|
adamc@41
|
438 p_sgn env sgn,
|
adamc@41
|
439 string ")",
|
adamc@41
|
440 space,
|
adamc@41
|
441 string ":",
|
adamc@41
|
442 space,
|
adamc@41
|
443 p_sgn env' sgn',
|
adamc@41
|
444 space,
|
adamc@41
|
445 string "=>",
|
adamc@41
|
446 space,
|
adamc@41
|
447 p_str env' str]
|
adamc@41
|
448 end
|
adamc@44
|
449 | StrApp (str1, str2) => box [p_str env str1,
|
adamc@44
|
450 string "(",
|
adamc@44
|
451 p_str env str2,
|
adamc@44
|
452 string ")"]
|
adamc@31
|
453 | StrError => string "<ERROR>"
|
adamc@3
|
454
|
adamc@32
|
455 and p_file env file =
|
adamc@3
|
456 let
|
adamc@31
|
457 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
458 (p_decl env d,
|
adamc@31
|
459 E.declBinds env d))
|
adamc@31
|
460 env file
|
adamc@3
|
461 in
|
adamc@3
|
462 p_list_sep newline (fn x => x) pds
|
adamc@3
|
463 end
|
adamc@3
|
464
|
adamc@3
|
465 end
|