adamc@38
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@38
|
2 * All rights reserved.
|
adamc@38
|
3 *
|
adamc@38
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@38
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@38
|
6 *
|
adamc@38
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@38
|
8 * this list of conditions and the following disclaimer.
|
adamc@38
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@38
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@38
|
11 * and/or other materials provided with the distribution.
|
adamc@38
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@38
|
13 * derived from this software without specific prior written permission.
|
adamc@38
|
14 *
|
adamc@38
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@38
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@38
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@38
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@38
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@38
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@38
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@38
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@38
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@38
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@38
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@38
|
26 *)
|
adamc@38
|
27
|
adamc@38
|
28 (* Pretty-printing elaborated Laconic/Web *)
|
adamc@38
|
29
|
adamc@38
|
30 structure ExplPrint :> EXPL_PRINT = struct
|
adamc@38
|
31
|
adamc@38
|
32 open Print.PD
|
adamc@38
|
33 open Print
|
adamc@38
|
34
|
adamc@38
|
35 open Expl
|
adamc@38
|
36
|
adamc@38
|
37 structure E = ExplEnv
|
adamc@38
|
38
|
adamc@38
|
39 val debug = ref false
|
adamc@38
|
40
|
adamc@38
|
41 fun p_kind' par (k, _) =
|
adamc@38
|
42 case k of
|
adamc@38
|
43 KType => string "Type"
|
adamc@38
|
44 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
|
adamc@38
|
45 space,
|
adamc@38
|
46 string "->",
|
adamc@38
|
47 space,
|
adamc@38
|
48 p_kind k2])
|
adamc@38
|
49 | KName => string "Name"
|
adamc@38
|
50 | KRecord k => box [string "{", p_kind k, string "}"]
|
adamc@87
|
51 | KUnit => string "Unit"
|
adamc@38
|
52
|
adamc@38
|
53 and p_kind k = p_kind' false k
|
adamc@38
|
54
|
adamc@38
|
55 fun p_con' par env (c, _) =
|
adamc@38
|
56 case c of
|
adamc@38
|
57 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
|
adamc@38
|
58 space,
|
adamc@38
|
59 string "->",
|
adamc@38
|
60 space,
|
adamc@38
|
61 p_con env t2])
|
adamc@38
|
62 | TCFun (x, k, c) => parenIf par (box [string x,
|
adamc@38
|
63 space,
|
adamc@38
|
64 string "::",
|
adamc@38
|
65 space,
|
adamc@38
|
66 p_kind k,
|
adamc@38
|
67 space,
|
adamc@38
|
68 string "->",
|
adamc@38
|
69 space,
|
adamc@38
|
70 p_con (E.pushCRel env x k) c])
|
adamc@38
|
71 | TRecord (CRecord (_, xcs), _) => box [string "{",
|
adamc@38
|
72 p_list (fn (x, c) =>
|
adamc@38
|
73 box [p_name env x,
|
adamc@38
|
74 space,
|
adamc@38
|
75 string ":",
|
adamc@38
|
76 space,
|
adamc@38
|
77 p_con env c]) xcs,
|
adamc@38
|
78 string "}"]
|
adamc@38
|
79 | TRecord c => box [string "$",
|
adamc@38
|
80 p_con' true env c]
|
adamc@38
|
81
|
adamc@38
|
82 | CRel n =>
|
adamc@38
|
83 if !debug then
|
adamc@38
|
84 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@38
|
85 else
|
adamc@38
|
86 string (#1 (E.lookupCRel env n))
|
adamc@38
|
87 | CNamed n =>
|
adamc@38
|
88 ((if !debug then
|
adamc@38
|
89 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@38
|
90 else
|
adamc@38
|
91 string (#1 (E.lookupCNamed env n)))
|
adamc@38
|
92 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@38
|
93 | CModProj (m1, ms, x) =>
|
adamc@38
|
94 let
|
adamc@38
|
95 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@38
|
96
|
adamc@38
|
97 val m1s = if !debug then
|
adamc@38
|
98 m1x ^ "__" ^ Int.toString m1
|
adamc@38
|
99 else
|
adamc@38
|
100 m1x
|
adamc@38
|
101 in
|
adamc@38
|
102 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@38
|
103 end
|
adamc@38
|
104
|
adamc@38
|
105 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@38
|
106 space,
|
adamc@38
|
107 p_con' true env c2])
|
adamc@38
|
108 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@38
|
109 space,
|
adamc@38
|
110 string x,
|
adamc@38
|
111 space,
|
adamc@38
|
112 string "::",
|
adamc@38
|
113 space,
|
adamc@38
|
114 p_kind k,
|
adamc@38
|
115 space,
|
adamc@38
|
116 string "=>",
|
adamc@38
|
117 space,
|
adamc@38
|
118 p_con (E.pushCRel env x k) c])
|
adamc@38
|
119
|
adamc@38
|
120 | CName s => box [string "#", string s]
|
adamc@38
|
121
|
adamc@38
|
122 | CRecord (k, xcs) =>
|
adamc@38
|
123 if !debug then
|
adamc@38
|
124 parenIf par (box [string "[",
|
adamc@38
|
125 p_list (fn (x, c) =>
|
adamc@38
|
126 box [p_con env x,
|
adamc@38
|
127 space,
|
adamc@38
|
128 string "=",
|
adamc@38
|
129 space,
|
adamc@38
|
130 p_con env c]) xcs,
|
adamc@38
|
131 string "]::",
|
adamc@38
|
132 p_kind k])
|
adamc@38
|
133 else
|
adamc@38
|
134 parenIf par (box [string "[",
|
adamc@38
|
135 p_list (fn (x, c) =>
|
adamc@38
|
136 box [p_con env x,
|
adamc@38
|
137 space,
|
adamc@38
|
138 string "=",
|
adamc@38
|
139 space,
|
adamc@38
|
140 p_con env c]) xcs,
|
adamc@38
|
141 string "]"])
|
adamc@38
|
142 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@38
|
143 space,
|
adamc@38
|
144 string "++",
|
adamc@38
|
145 space,
|
adamc@38
|
146 p_con env c2])
|
adamc@68
|
147 | CFold _ => string "fold"
|
adamc@87
|
148 | CUnit => string "()"
|
adamc@38
|
149
|
adamc@38
|
150 and p_con env = p_con' false env
|
adamc@38
|
151
|
adamc@38
|
152 and p_name env (all as (c, _)) =
|
adamc@38
|
153 case c of
|
adamc@38
|
154 CName s => string s
|
adamc@38
|
155 | _ => p_con env all
|
adamc@38
|
156
|
adamc@38
|
157 fun p_exp' par env (e, _) =
|
adamc@38
|
158 case e of
|
adamc@38
|
159 EPrim p => Prim.p_t p
|
adamc@38
|
160 | ERel n =>
|
adamc@38
|
161 if !debug then
|
adamc@38
|
162 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@38
|
163 else
|
adamc@38
|
164 string (#1 (E.lookupERel env n))
|
adamc@38
|
165 | ENamed n =>
|
adamc@38
|
166 if !debug then
|
adamc@38
|
167 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@38
|
168 else
|
adamc@38
|
169 string (#1 (E.lookupENamed env n))
|
adamc@38
|
170 | EModProj (m1, ms, x) =>
|
adamc@38
|
171 let
|
adamc@38
|
172 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@38
|
173
|
adamc@38
|
174 val m1s = if !debug then
|
adamc@38
|
175 m1x ^ "__" ^ Int.toString m1
|
adamc@38
|
176 else
|
adamc@38
|
177 m1x
|
adamc@38
|
178 in
|
adamc@38
|
179 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@38
|
180 end
|
adamc@38
|
181
|
adamc@38
|
182 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@38
|
183 space,
|
adamc@38
|
184 p_exp' true env e2])
|
adamc@38
|
185 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@38
|
186 space,
|
adamc@38
|
187 string x,
|
adamc@38
|
188 space,
|
adamc@38
|
189 string ":",
|
adamc@38
|
190 space,
|
adamc@38
|
191 p_con env t,
|
adamc@38
|
192 space,
|
adamc@38
|
193 string "=>",
|
adamc@38
|
194 space,
|
adamc@38
|
195 p_exp (E.pushERel env x t) e])
|
adamc@38
|
196 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@38
|
197 space,
|
adamc@38
|
198 string "[",
|
adamc@38
|
199 p_con env c,
|
adamc@38
|
200 string "]"])
|
adamc@38
|
201 | ECAbs (x, k, e) => parenIf par (box [string "fn",
|
adamc@38
|
202 space,
|
adamc@38
|
203 string x,
|
adamc@38
|
204 space,
|
adamc@38
|
205 string "::",
|
adamc@38
|
206 space,
|
adamc@38
|
207 p_kind k,
|
adamc@38
|
208 space,
|
adamc@38
|
209 string "=>",
|
adamc@38
|
210 space,
|
adamc@38
|
211 p_exp (E.pushCRel env x k) e])
|
adamc@38
|
212
|
adamc@38
|
213 | ERecord xes => box [string "{",
|
adamc@38
|
214 p_list (fn (x, e, _) =>
|
adamc@38
|
215 box [p_name env x,
|
adamc@38
|
216 space,
|
adamc@38
|
217 string "=",
|
adamc@38
|
218 space,
|
adamc@38
|
219 p_exp env e]) xes,
|
adamc@38
|
220 string "}"]
|
adamc@38
|
221 | EField (e, c, {field, rest}) =>
|
adamc@38
|
222 if !debug then
|
adamc@38
|
223 box [p_exp' true env e,
|
adamc@38
|
224 string ".",
|
adamc@38
|
225 p_con' true env c,
|
adamc@38
|
226 space,
|
adamc@38
|
227 string "[",
|
adamc@38
|
228 p_con env field,
|
adamc@38
|
229 space,
|
adamc@38
|
230 string " in ",
|
adamc@38
|
231 space,
|
adamc@38
|
232 p_con env rest,
|
adamc@38
|
233 string "]"]
|
adamc@38
|
234 else
|
adamc@38
|
235 box [p_exp' true env e,
|
adamc@38
|
236 string ".",
|
adamc@38
|
237 p_con' true env c]
|
adamc@72
|
238 | EFold _ => string "fold"
|
adamc@38
|
239
|
adamc@38
|
240 and p_exp env = p_exp' false env
|
adamc@38
|
241
|
adamc@38
|
242 fun p_named x n =
|
adamc@38
|
243 if !debug then
|
adamc@38
|
244 box [string x,
|
adamc@38
|
245 string "__",
|
adamc@38
|
246 string (Int.toString n)]
|
adamc@38
|
247 else
|
adamc@38
|
248 string x
|
adamc@38
|
249
|
adamc@38
|
250 fun p_sgn_item env (sgi, _) =
|
adamc@38
|
251 case sgi of
|
adamc@38
|
252 SgiConAbs (x, n, k) => box [string "con",
|
adamc@38
|
253 space,
|
adamc@38
|
254 p_named x n,
|
adamc@38
|
255 space,
|
adamc@38
|
256 string "::",
|
adamc@38
|
257 space,
|
adamc@38
|
258 p_kind k]
|
adamc@38
|
259 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@38
|
260 space,
|
adamc@38
|
261 p_named x n,
|
adamc@38
|
262 space,
|
adamc@38
|
263 string "::",
|
adamc@38
|
264 space,
|
adamc@38
|
265 p_kind k,
|
adamc@38
|
266 space,
|
adamc@38
|
267 string "=",
|
adamc@38
|
268 space,
|
adamc@38
|
269 p_con env c]
|
adamc@38
|
270 | SgiVal (x, n, c) => box [string "val",
|
adamc@38
|
271 space,
|
adamc@38
|
272 p_named x n,
|
adamc@38
|
273 space,
|
adamc@38
|
274 string ":",
|
adamc@38
|
275 space,
|
adamc@38
|
276 p_con env c]
|
adamc@38
|
277 | SgiStr (x, n, sgn) => box [string "structure",
|
adamc@38
|
278 space,
|
adamc@38
|
279 p_named x n,
|
adamc@38
|
280 space,
|
adamc@38
|
281 string ":",
|
adamc@38
|
282 space,
|
adamc@38
|
283 p_sgn env sgn]
|
adamc@64
|
284 | SgiSgn (x, n, sgn) => box [string "signature",
|
adamc@64
|
285 space,
|
adamc@64
|
286 p_named x n,
|
adamc@64
|
287 space,
|
adamc@64
|
288 string "=",
|
adamc@64
|
289 space,
|
adamc@64
|
290 p_sgn env sgn]
|
adamc@38
|
291
|
adamc@38
|
292 and p_sgn env (sgn, _) =
|
adamc@38
|
293 case sgn of
|
adamc@38
|
294 SgnConst sgis => box [string "sig",
|
adamc@38
|
295 newline,
|
adamc@38
|
296 let
|
adamc@38
|
297 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
|
adamc@38
|
298 (p_sgn_item env sgi,
|
adamc@38
|
299 E.sgiBinds env sgi))
|
adamc@38
|
300 env sgis
|
adamc@38
|
301 in
|
adamc@38
|
302 p_list_sep newline (fn x => x) psgis
|
adamc@38
|
303 end,
|
adamc@38
|
304 newline,
|
adamc@38
|
305 string "end"]
|
adamc@38
|
306 | SgnVar n => string (#1 (E.lookupSgnNamed env n))
|
adamc@45
|
307 | SgnFun (x, n, sgn, sgn') => box [string "functor",
|
adamc@45
|
308 space,
|
adamc@45
|
309 string "(",
|
adamc@45
|
310 string x,
|
adamc@45
|
311 space,
|
adamc@45
|
312 string ":",
|
adamc@45
|
313 space,
|
adamc@45
|
314 p_sgn env sgn,
|
adamc@45
|
315 string ")",
|
adamc@45
|
316 space,
|
adamc@45
|
317 string ":",
|
adamc@45
|
318 space,
|
adamc@45
|
319 p_sgn (E.pushStrNamed env x n sgn) sgn']
|
adamc@45
|
320 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
|
adamc@45
|
321 space,
|
adamc@45
|
322 string "where",
|
adamc@45
|
323 space,
|
adamc@45
|
324 string "con",
|
adamc@45
|
325 space,
|
adamc@45
|
326 string x,
|
adamc@45
|
327 space,
|
adamc@45
|
328 string "=",
|
adamc@45
|
329 space,
|
adamc@45
|
330 p_con env c]
|
adamc@64
|
331 | SgnProj (m1, ms, x) =>
|
adamc@64
|
332 let
|
adamc@64
|
333 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@64
|
334
|
adamc@64
|
335 val m1s = if !debug then
|
adamc@64
|
336 m1x ^ "__" ^ Int.toString m1
|
adamc@64
|
337 else
|
adamc@64
|
338 m1x
|
adamc@64
|
339 in
|
adamc@64
|
340 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@64
|
341 end
|
adamc@38
|
342
|
adamc@38
|
343 fun p_decl env ((d, _) : decl) =
|
adamc@38
|
344 case d of
|
adamc@38
|
345 DCon (x, n, k, c) => box [string "con",
|
adamc@38
|
346 space,
|
adamc@38
|
347 p_named x n,
|
adamc@38
|
348 space,
|
adamc@38
|
349 string "::",
|
adamc@38
|
350 space,
|
adamc@38
|
351 p_kind k,
|
adamc@38
|
352 space,
|
adamc@38
|
353 string "=",
|
adamc@38
|
354 space,
|
adamc@38
|
355 p_con env c]
|
adamc@38
|
356 | DVal (x, n, t, e) => box [string "val",
|
adamc@38
|
357 space,
|
adamc@38
|
358 p_named x n,
|
adamc@38
|
359 space,
|
adamc@38
|
360 string ":",
|
adamc@38
|
361 space,
|
adamc@38
|
362 p_con env t,
|
adamc@38
|
363 space,
|
adamc@38
|
364 string "=",
|
adamc@38
|
365 space,
|
adamc@38
|
366 p_exp env e]
|
adamc@38
|
367
|
adamc@38
|
368 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@38
|
369 space,
|
adamc@38
|
370 p_named x n,
|
adamc@38
|
371 space,
|
adamc@38
|
372 string "=",
|
adamc@38
|
373 space,
|
adamc@38
|
374 p_sgn env sgn]
|
adamc@38
|
375 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@38
|
376 space,
|
adamc@38
|
377 p_named x n,
|
adamc@38
|
378 space,
|
adamc@38
|
379 string ":",
|
adamc@38
|
380 space,
|
adamc@38
|
381 p_sgn env sgn,
|
adamc@38
|
382 space,
|
adamc@38
|
383 string "=",
|
adamc@38
|
384 space,
|
adamc@38
|
385 p_str env str]
|
adamc@48
|
386 | DFfiStr (x, n, sgn) => box [string "extern",
|
adamc@48
|
387 space,
|
adamc@48
|
388 string "structure",
|
adamc@48
|
389 space,
|
adamc@48
|
390 p_named x n,
|
adamc@48
|
391 space,
|
adamc@48
|
392 string ":",
|
adamc@48
|
393 space,
|
adamc@48
|
394 p_sgn env sgn]
|
adamc@38
|
395
|
adamc@38
|
396 and p_str env (str, _) =
|
adamc@38
|
397 case str of
|
adamc@38
|
398 StrConst ds => box [string "struct",
|
adamc@38
|
399 newline,
|
adamc@38
|
400 p_file env ds,
|
adamc@38
|
401 newline,
|
adamc@38
|
402 string "end"]
|
adamc@38
|
403 | StrVar n => string (#1 (E.lookupStrNamed env n))
|
adamc@38
|
404 | StrProj (str, s) => box [p_str env str,
|
adamc@38
|
405 string ".",
|
adamc@38
|
406 string s]
|
adamc@45
|
407 | StrFun (x, n, sgn, sgn', str) =>
|
adamc@45
|
408 let
|
adamc@45
|
409 val env' = E.pushStrNamed env x n sgn
|
adamc@45
|
410 in
|
adamc@45
|
411 box [string "functor",
|
adamc@45
|
412 space,
|
adamc@45
|
413 string "(",
|
adamc@45
|
414 string x,
|
adamc@45
|
415 space,
|
adamc@45
|
416 string ":",
|
adamc@45
|
417 space,
|
adamc@45
|
418 p_sgn env sgn,
|
adamc@45
|
419 string ")",
|
adamc@45
|
420 space,
|
adamc@45
|
421 string ":",
|
adamc@45
|
422 space,
|
adamc@45
|
423 p_sgn env' sgn',
|
adamc@45
|
424 space,
|
adamc@45
|
425 string "=>",
|
adamc@45
|
426 space,
|
adamc@45
|
427 p_str env' str]
|
adamc@45
|
428 end
|
adamc@45
|
429 | StrApp (str1, str2) => box [p_str env str1,
|
adamc@45
|
430 string "(",
|
adamc@45
|
431 p_str env str2,
|
adamc@45
|
432 string ")"]
|
adamc@38
|
433
|
adamc@38
|
434 and p_file env file =
|
adamc@38
|
435 let
|
adamc@38
|
436 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@38
|
437 (p_decl env d,
|
adamc@38
|
438 E.declBinds env d))
|
adamc@38
|
439 env file
|
adamc@38
|
440 in
|
adamc@38
|
441 p_list_sep newline (fn x => x) pds
|
adamc@38
|
442 end
|
adamc@38
|
443
|
adamc@38
|
444 end
|