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