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@11
|
96 if !debug then
|
adamc@11
|
97 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@11
|
98 else
|
adamc@11
|
99 string (#1 (E.lookupCNamed env n))
|
adamc@3
|
100
|
adamc@3
|
101 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@3
|
102 space,
|
adamc@3
|
103 p_con' true env c2])
|
adamc@8
|
104 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@8
|
105 space,
|
adamc@8
|
106 string x,
|
adamc@8
|
107 space,
|
adamc@8
|
108 string "::",
|
adamc@8
|
109 space,
|
adamc@8
|
110 p_kind k,
|
adamc@8
|
111 space,
|
adamc@8
|
112 string "=>",
|
adamc@8
|
113 space,
|
adamc@8
|
114 p_con (E.pushCRel env x k) c])
|
adamc@3
|
115
|
adamc@3
|
116 | CName s => box [string "#", string s]
|
adamc@3
|
117
|
adamc@12
|
118 | CRecord (k, xcs) =>
|
adamc@12
|
119 if !debug then
|
adamc@12
|
120 parenIf par (box [string "[",
|
adamc@12
|
121 p_list (fn (x, c) =>
|
adamc@12
|
122 box [p_con env x,
|
adamc@12
|
123 space,
|
adamc@12
|
124 string "=",
|
adamc@12
|
125 space,
|
adamc@12
|
126 p_con env c]) xcs,
|
adamc@12
|
127 string "]::",
|
adamc@12
|
128 p_kind k])
|
adamc@12
|
129 else
|
adamc@12
|
130 parenIf par (box [string "[",
|
adamc@12
|
131 p_list (fn (x, c) =>
|
adamc@12
|
132 box [p_con env x,
|
adamc@12
|
133 space,
|
adamc@12
|
134 string "=",
|
adamc@12
|
135 space,
|
adamc@12
|
136 p_con env c]) xcs,
|
adamc@12
|
137 string "]"])
|
adamc@3
|
138 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@3
|
139 space,
|
adamc@3
|
140 string "++",
|
adamc@3
|
141 space,
|
adamc@3
|
142 p_con env c2])
|
adamc@3
|
143
|
adamc@3
|
144 | CError => string "<ERROR>"
|
adamc@6
|
145 | CUnif (_, _, ref (SOME c)) => p_con' par env c
|
adamc@6
|
146 | CUnif (k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
|
adamc@6
|
147 p_kind k,
|
adamc@6
|
148 string ">"]
|
adamc@3
|
149
|
adamc@3
|
150 and p_con env = p_con' false env
|
adamc@3
|
151
|
adamc@20
|
152 and p_name env (all as (c, _)) =
|
adamc@20
|
153 case c of
|
adamc@20
|
154 CName s => string s
|
adamc@20
|
155 | _ => p_con env all
|
adamc@20
|
156
|
adamc@9
|
157 fun p_exp' par env (e, _) =
|
adamc@9
|
158 case e of
|
adamc@14
|
159 EPrim p => Prim.p_t p
|
adamc@14
|
160 | ERel n =>
|
adamc@11
|
161 if !debug then
|
adamc@11
|
162 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@11
|
163 else
|
adamc@11
|
164 string (#1 (E.lookupERel env n))
|
adamc@11
|
165 | ENamed n =>
|
adamc@11
|
166 if !debug then
|
adamc@11
|
167 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@11
|
168 else
|
adamc@11
|
169 string (#1 (E.lookupENamed env n))
|
adamc@9
|
170 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@9
|
171 space,
|
adamc@9
|
172 p_exp' true env e2])
|
adamc@26
|
173 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@26
|
174 space,
|
adamc@26
|
175 string x,
|
adamc@26
|
176 space,
|
adamc@26
|
177 string ":",
|
adamc@26
|
178 space,
|
adamc@26
|
179 p_con env t,
|
adamc@26
|
180 space,
|
adamc@26
|
181 string "=>",
|
adamc@26
|
182 space,
|
adamc@26
|
183 p_exp (E.pushERel env x t) e])
|
adamc@9
|
184 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@9
|
185 space,
|
adamc@9
|
186 string "[",
|
adamc@9
|
187 p_con env c,
|
adamc@9
|
188 string "]"])
|
adamc@9
|
189 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@9
|
190 space,
|
adamc@9
|
191 string x,
|
adamc@9
|
192 space,
|
adamc@9
|
193 p_explicitness exp,
|
adamc@9
|
194 space,
|
adamc@9
|
195 p_kind k,
|
adamc@9
|
196 space,
|
adamc@9
|
197 string "=>",
|
adamc@9
|
198 space,
|
adamc@9
|
199 p_exp (E.pushCRel env x k) e])
|
adamc@9
|
200
|
adamc@12
|
201 | ERecord xes => box [string "{",
|
adamc@29
|
202 p_list (fn (x, e, _) =>
|
adamc@21
|
203 box [p_name env x,
|
adamc@12
|
204 space,
|
adamc@12
|
205 string "=",
|
adamc@12
|
206 space,
|
adamc@12
|
207 p_exp env e]) xes,
|
adamc@12
|
208 string "}"]
|
adamc@12
|
209 | EField (e, c, {field, rest}) =>
|
adamc@12
|
210 if !debug then
|
adamc@12
|
211 box [p_exp' true env e,
|
adamc@12
|
212 string ".",
|
adamc@12
|
213 p_con' true env c,
|
adamc@12
|
214 space,
|
adamc@12
|
215 string "[",
|
adamc@12
|
216 p_con env field,
|
adamc@12
|
217 space,
|
adamc@12
|
218 string " in ",
|
adamc@12
|
219 space,
|
adamc@12
|
220 p_con env rest,
|
adamc@12
|
221 string "]"]
|
adamc@12
|
222 else
|
adamc@12
|
223 box [p_exp' true env e,
|
adamc@12
|
224 string ".",
|
adamc@12
|
225 p_con' true env c]
|
adamc@12
|
226
|
adamc@9
|
227 | EError => string "<ERROR>"
|
adamc@9
|
228
|
adamc@9
|
229 and p_exp env = p_exp' false env
|
adamc@9
|
230
|
adamc@31
|
231 fun p_named x n =
|
adamc@31
|
232 if !debug then
|
adamc@31
|
233 box [string x,
|
adamc@31
|
234 string "__",
|
adamc@31
|
235 string (Int.toString n)]
|
adamc@31
|
236 else
|
adamc@31
|
237 string x
|
adamc@31
|
238
|
adamc@31
|
239 fun p_sgn_item env (sgi, _) =
|
adamc@31
|
240 case sgi of
|
adamc@31
|
241 SgiConAbs (x, n, k) => box [string "con",
|
adamc@31
|
242 space,
|
adamc@31
|
243 p_named x n,
|
adamc@31
|
244 space,
|
adamc@31
|
245 string "::",
|
adamc@31
|
246 space,
|
adamc@31
|
247 p_kind k]
|
adamc@31
|
248 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@31
|
249 space,
|
adamc@31
|
250 p_named x n,
|
adamc@31
|
251 space,
|
adamc@31
|
252 string "::",
|
adamc@31
|
253 space,
|
adamc@31
|
254 p_kind k,
|
adamc@31
|
255 space,
|
adamc@31
|
256 string "=",
|
adamc@31
|
257 space,
|
adamc@31
|
258 p_con env c]
|
adamc@31
|
259 | SgiVal (x, n, c) => box [string "val",
|
adamc@31
|
260 space,
|
adamc@31
|
261 p_named x n,
|
adamc@31
|
262 space,
|
adamc@31
|
263 string ":",
|
adamc@31
|
264 space,
|
adamc@31
|
265 p_con env c]
|
adamc@31
|
266 | SgiStr (x, n, sgn) => box [string "structure",
|
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_sgn env sgn]
|
adamc@31
|
273
|
adamc@31
|
274 and p_sgn env (sgn, _) =
|
adamc@31
|
275 case sgn of
|
adamc@31
|
276 SgnConst sgis => box [string "sig",
|
adamc@31
|
277 newline,
|
adamc@31
|
278 p_list_sep newline (p_sgn_item env) sgis,
|
adamc@31
|
279 newline,
|
adamc@31
|
280 string "end"]
|
adamc@31
|
281 | SgnVar n => string (#1 (E.lookupSgnNamed env n))
|
adamc@31
|
282 | SgnError => string "<ERROR>"
|
adamc@31
|
283
|
adamc@3
|
284 fun p_decl env ((d, _) : decl) =
|
adamc@3
|
285 case d of
|
adamc@31
|
286 DCon (x, n, k, c) => box [string "con",
|
adamc@31
|
287 space,
|
adamc@31
|
288 p_named x n,
|
adamc@31
|
289 space,
|
adamc@31
|
290 string "::",
|
adamc@31
|
291 space,
|
adamc@31
|
292 p_kind k,
|
adamc@31
|
293 space,
|
adamc@31
|
294 string "=",
|
adamc@31
|
295 space,
|
adamc@31
|
296 p_con env c]
|
adamc@31
|
297 | DVal (x, n, t, e) => box [string "val",
|
adamc@31
|
298 space,
|
adamc@31
|
299 p_named x n,
|
adamc@31
|
300 space,
|
adamc@31
|
301 string ":",
|
adamc@31
|
302 space,
|
adamc@31
|
303 p_con env t,
|
adamc@31
|
304 space,
|
adamc@31
|
305 string "=",
|
adamc@31
|
306 space,
|
adamc@31
|
307 p_exp env e]
|
adamc@31
|
308
|
adamc@31
|
309 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@31
|
310 space,
|
adamc@31
|
311 p_named x n,
|
adamc@31
|
312 space,
|
adamc@31
|
313 string "=",
|
adamc@31
|
314 space,
|
adamc@31
|
315 p_sgn env sgn]
|
adamc@31
|
316 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@31
|
317 space,
|
adamc@31
|
318 p_named x n,
|
adamc@31
|
319 space,
|
adamc@31
|
320 string ":",
|
adamc@31
|
321 space,
|
adamc@31
|
322 p_sgn env sgn,
|
adamc@31
|
323 space,
|
adamc@31
|
324 string "=",
|
adamc@31
|
325 space,
|
adamc@31
|
326 p_str env str]
|
adamc@31
|
327
|
adamc@31
|
328 and p_str env (str, _) =
|
adamc@31
|
329 case str of
|
adamc@31
|
330 StrConst ds => box [string "struct",
|
adamc@31
|
331 newline,
|
adamc@31
|
332 p_list_sep newline (p_decl env) ds,
|
adamc@31
|
333 newline,
|
adamc@31
|
334 string "end"]
|
adamc@31
|
335 | StrVar n => string (#1 (E.lookupStrNamed env n))
|
adamc@31
|
336 | StrError => string "<ERROR>"
|
adamc@3
|
337
|
adamc@3
|
338 fun p_file env file =
|
adamc@3
|
339 let
|
adamc@31
|
340 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
341 (p_decl env d,
|
adamc@31
|
342 E.declBinds env d))
|
adamc@31
|
343 env file
|
adamc@3
|
344 in
|
adamc@3
|
345 p_list_sep newline (fn x => x) pds
|
adamc@3
|
346 end
|
adamc@3
|
347
|
adamc@3
|
348 end
|