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@85
|
80 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con env c1,
|
adamc@85
|
81 space,
|
adamc@85
|
82 string "~",
|
adamc@85
|
83 space,
|
adamc@85
|
84 p_con env c2,
|
adamc@85
|
85 space,
|
adamc@85
|
86 string "->",
|
adamc@85
|
87 space,
|
adamc@85
|
88 p_con env c3])
|
adamc@3
|
89 | TRecord (CRecord (_, xcs), _) => box [string "{",
|
adamc@3
|
90 p_list (fn (x, c) =>
|
adamc@20
|
91 box [p_name env x,
|
adamc@3
|
92 space,
|
adamc@3
|
93 string ":",
|
adamc@3
|
94 space,
|
adamc@3
|
95 p_con env c]) xcs,
|
adamc@3
|
96 string "}"]
|
adamc@3
|
97 | TRecord c => box [string "$",
|
adamc@3
|
98 p_con' true env c]
|
adamc@3
|
99
|
adamc@11
|
100 | CRel n =>
|
adamc@71
|
101 ((if !debug then
|
adamc@71
|
102 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@71
|
103 else
|
adamc@71
|
104 string (#1 (E.lookupCRel env n)))
|
adamc@71
|
105 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@11
|
106 | CNamed n =>
|
adamc@34
|
107 ((if !debug then
|
adamc@34
|
108 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@34
|
109 else
|
adamc@34
|
110 string (#1 (E.lookupCNamed env n)))
|
adamc@34
|
111 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@34
|
112 | CModProj (m1, ms, x) =>
|
adamc@34
|
113 let
|
adamc@88
|
114 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
115 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@88
|
116
|
adamc@34
|
117 val m1s = if !debug then
|
adamc@34
|
118 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
119 else
|
adamc@34
|
120 m1x
|
adamc@34
|
121 in
|
adamc@34
|
122 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@88
|
123 end
|
adamc@3
|
124
|
adamc@3
|
125 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@3
|
126 space,
|
adamc@3
|
127 p_con' true env c2])
|
adamc@147
|
128 | CAbs (x, k, c) => parenIf true (box [string "fn",
|
adamc@147
|
129 space,
|
adamc@147
|
130 string x,
|
adamc@147
|
131 space,
|
adamc@147
|
132 string "::",
|
adamc@147
|
133 space,
|
adamc@147
|
134 p_kind k,
|
adamc@147
|
135 space,
|
adamc@147
|
136 string "=>",
|
adamc@147
|
137 space,
|
adamc@147
|
138 p_con (E.pushCRel env x k) c])
|
adamc@84
|
139 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con env c1,
|
adamc@84
|
140 space,
|
adamc@84
|
141 string "~",
|
adamc@84
|
142 space,
|
adamc@84
|
143 p_con env c2,
|
adamc@84
|
144 space,
|
adamc@84
|
145 string "=>",
|
adamc@84
|
146 space,
|
adamc@84
|
147 p_con env c3])
|
adamc@3
|
148
|
adamc@3
|
149 | CName s => box [string "#", string s]
|
adamc@3
|
150
|
adamc@12
|
151 | CRecord (k, xcs) =>
|
adamc@12
|
152 if !debug then
|
adamc@12
|
153 parenIf par (box [string "[",
|
adamc@12
|
154 p_list (fn (x, c) =>
|
adamc@12
|
155 box [p_con env x,
|
adamc@12
|
156 space,
|
adamc@12
|
157 string "=",
|
adamc@12
|
158 space,
|
adamc@12
|
159 p_con env c]) xcs,
|
adamc@12
|
160 string "]::",
|
adamc@12
|
161 p_kind k])
|
adamc@12
|
162 else
|
adamc@12
|
163 parenIf par (box [string "[",
|
adamc@12
|
164 p_list (fn (x, c) =>
|
adamc@12
|
165 box [p_con env x,
|
adamc@12
|
166 space,
|
adamc@12
|
167 string "=",
|
adamc@12
|
168 space,
|
adamc@12
|
169 p_con env c]) xcs,
|
adamc@12
|
170 string "]"])
|
adamc@3
|
171 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@3
|
172 space,
|
adamc@3
|
173 string "++",
|
adamc@3
|
174 space,
|
adamc@3
|
175 p_con env c2])
|
adamc@67
|
176 | CFold _ => string "fold"
|
adamc@3
|
177
|
adamc@82
|
178 | CUnit => string "()"
|
adamc@82
|
179
|
adamc@3
|
180 | CError => string "<ERROR>"
|
adamc@76
|
181 | CUnif (_, _, _, ref (SOME c)) => p_con' par env c
|
adamc@76
|
182 | CUnif (_, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
|
adamc@76
|
183 p_kind k,
|
adamc@76
|
184 string ">"]
|
adamc@3
|
185
|
adamc@3
|
186 and p_con env = p_con' false env
|
adamc@3
|
187
|
adamc@20
|
188 and p_name env (all as (c, _)) =
|
adamc@20
|
189 case c of
|
adamc@20
|
190 CName s => string s
|
adamc@20
|
191 | _ => p_con env all
|
adamc@20
|
192
|
adamc@171
|
193 fun p_patCon env pc =
|
adamc@171
|
194 case pc of
|
adamc@171
|
195 PConVar n =>
|
adamc@171
|
196 ((if !debug then
|
adamc@171
|
197 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@171
|
198 else
|
adamc@171
|
199 string (#1 (E.lookupENamed env n)))
|
adamc@171
|
200 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@171
|
201 | PConProj (m1, ms, x) =>
|
adamc@171
|
202 let
|
adamc@171
|
203 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@171
|
204 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@171
|
205
|
adamc@171
|
206 val m1s = if !debug then
|
adamc@171
|
207 m1x ^ "__" ^ Int.toString m1
|
adamc@171
|
208 else
|
adamc@171
|
209 m1x
|
adamc@171
|
210 in
|
adamc@171
|
211 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@171
|
212 end
|
adamc@171
|
213
|
adamc@171
|
214 fun p_pat' par env (p, _) =
|
adamc@171
|
215 case p of
|
adamc@171
|
216 PWild => string "_"
|
adamc@171
|
217 | PVar s => string s
|
adamc@173
|
218 | PPrim p => Prim.p_t p
|
adamc@171
|
219 | PCon (pc, NONE) => p_patCon env pc
|
adamc@171
|
220 | PCon (pc, SOME p) => parenIf par (box [p_patCon env pc,
|
adamc@171
|
221 space,
|
adamc@171
|
222 p_pat' true env p])
|
adamc@175
|
223 | PRecord (xps, flex) =>
|
adamc@175
|
224 let
|
adamc@175
|
225 val pps = map (fn (x, p) => box [string x, space, string "=", space, p_pat env p]) xps
|
adamc@175
|
226 in
|
adamc@175
|
227 box [string "{",
|
adamc@175
|
228 p_list_sep (box [string ",", space]) (fn x => x)
|
adamc@175
|
229 (case flex of
|
adamc@175
|
230 NONE => pps
|
adamc@175
|
231 | SOME _ => pps @ [string "..."]),
|
adamc@175
|
232 string "}"]
|
adamc@175
|
233 end
|
adamc@171
|
234
|
adamc@175
|
235 and p_pat x = p_pat' false x
|
adamc@171
|
236
|
adamc@9
|
237 fun p_exp' par env (e, _) =
|
adamc@9
|
238 case e of
|
adamc@14
|
239 EPrim p => Prim.p_t p
|
adamc@14
|
240 | ERel n =>
|
adamc@88
|
241 ((if !debug then
|
adamc@88
|
242 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@88
|
243 else
|
adamc@88
|
244 string (#1 (E.lookupERel env n)))
|
adamc@88
|
245 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@11
|
246 | ENamed n =>
|
adamc@88
|
247 ((if !debug then
|
adamc@88
|
248 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@88
|
249 else
|
adamc@88
|
250 string (#1 (E.lookupENamed env n)))
|
adamc@88
|
251 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@34
|
252 | EModProj (m1, ms, x) =>
|
adamc@34
|
253 let
|
adamc@88
|
254 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
255 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@88
|
256
|
adamc@34
|
257 val m1s = if !debug then
|
adamc@34
|
258 m1x ^ "__" ^ Int.toString m1
|
adamc@34
|
259 else
|
adamc@34
|
260 m1x
|
adamc@34
|
261 in
|
adamc@34
|
262 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@34
|
263 end
|
adamc@34
|
264
|
adamc@9
|
265 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@9
|
266 space,
|
adamc@9
|
267 p_exp' true env e2])
|
adamc@26
|
268 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@26
|
269 space,
|
adamc@26
|
270 string x,
|
adamc@26
|
271 space,
|
adamc@26
|
272 string ":",
|
adamc@26
|
273 space,
|
adamc@26
|
274 p_con env t,
|
adamc@26
|
275 space,
|
adamc@26
|
276 string "=>",
|
adamc@26
|
277 space,
|
adamc@26
|
278 p_exp (E.pushERel env x t) e])
|
adamc@9
|
279 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@9
|
280 space,
|
adamc@9
|
281 string "[",
|
adamc@9
|
282 p_con env c,
|
adamc@9
|
283 string "]"])
|
adamc@9
|
284 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
|
adamc@9
|
285 space,
|
adamc@9
|
286 string x,
|
adamc@9
|
287 space,
|
adamc@9
|
288 p_explicitness exp,
|
adamc@9
|
289 space,
|
adamc@9
|
290 p_kind k,
|
adamc@9
|
291 space,
|
adamc@9
|
292 string "=>",
|
adamc@9
|
293 space,
|
adamc@9
|
294 p_exp (E.pushCRel env x k) e])
|
adamc@9
|
295
|
adamc@12
|
296 | ERecord xes => box [string "{",
|
adamc@29
|
297 p_list (fn (x, e, _) =>
|
adamc@21
|
298 box [p_name env x,
|
adamc@12
|
299 space,
|
adamc@12
|
300 string "=",
|
adamc@12
|
301 space,
|
adamc@12
|
302 p_exp env e]) xes,
|
adamc@12
|
303 string "}"]
|
adamc@12
|
304 | EField (e, c, {field, rest}) =>
|
adamc@12
|
305 if !debug then
|
adamc@12
|
306 box [p_exp' true env e,
|
adamc@12
|
307 string ".",
|
adamc@12
|
308 p_con' true env c,
|
adamc@12
|
309 space,
|
adamc@12
|
310 string "[",
|
adamc@12
|
311 p_con env field,
|
adamc@12
|
312 space,
|
adamc@12
|
313 string " in ",
|
adamc@12
|
314 space,
|
adamc@12
|
315 p_con env rest,
|
adamc@12
|
316 string "]"]
|
adamc@12
|
317 else
|
adamc@12
|
318 box [p_exp' true env e,
|
adamc@12
|
319 string ".",
|
adamc@12
|
320 p_con' true env c]
|
adamc@149
|
321 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
322 parenIf par (if !debug then
|
adamc@149
|
323 box [p_exp' true env e,
|
adamc@149
|
324 space,
|
adamc@149
|
325 string "--",
|
adamc@149
|
326 space,
|
adamc@149
|
327 p_con' true env c,
|
adamc@149
|
328 space,
|
adamc@149
|
329 string "[",
|
adamc@149
|
330 p_con env field,
|
adamc@149
|
331 space,
|
adamc@149
|
332 string " in ",
|
adamc@149
|
333 space,
|
adamc@149
|
334 p_con env rest,
|
adamc@149
|
335 string "]"]
|
adamc@149
|
336 else
|
adamc@149
|
337 box [p_exp' true env e,
|
adamc@149
|
338 space,
|
adamc@149
|
339 string "--",
|
adamc@149
|
340 space,
|
adamc@149
|
341 p_con' true env c])
|
adamc@72
|
342 | EFold _ => string "fold"
|
adamc@71
|
343
|
adamc@171
|
344 | ECase (e, pes, _) => parenIf par (box [string "case",
|
adamc@171
|
345 space,
|
adamc@171
|
346 p_exp env e,
|
adamc@171
|
347 space,
|
adamc@171
|
348 string "of",
|
adamc@171
|
349 space,
|
adamc@171
|
350 p_list_sep (box [space, string "|", space])
|
adamc@171
|
351 (fn (p, e) => box [p_pat env p,
|
adamc@171
|
352 space,
|
adamc@171
|
353 string "=>",
|
adamc@171
|
354 space,
|
adamc@171
|
355 p_exp env e]) pes])
|
adamc@171
|
356
|
adamc@9
|
357 | EError => string "<ERROR>"
|
adamc@9
|
358
|
adamc@9
|
359 and p_exp env = p_exp' false env
|
adamc@9
|
360
|
adamc@31
|
361 fun p_named x n =
|
adamc@31
|
362 if !debug then
|
adamc@31
|
363 box [string x,
|
adamc@31
|
364 string "__",
|
adamc@31
|
365 string (Int.toString n)]
|
adamc@31
|
366 else
|
adamc@31
|
367 string x
|
adamc@31
|
368
|
adamc@156
|
369 fun p_datatype env (x, n, cons) =
|
adamc@156
|
370 let
|
adamc@156
|
371 val env = E.pushCNamedAs env x n (KType, ErrorMsg.dummySpan) NONE
|
adamc@156
|
372 in
|
adamc@156
|
373 box [string "datatype",
|
adamc@156
|
374 space,
|
adamc@156
|
375 string x,
|
adamc@156
|
376 space,
|
adamc@156
|
377 string "=",
|
adamc@156
|
378 space,
|
adamc@156
|
379 p_list_sep (box [space, string "|", space])
|
adamc@156
|
380 (fn (x, _, NONE) => string x
|
adamc@156
|
381 | (x, _, SOME t) => box [string x, space, string "of", space, p_con env t])
|
adamc@156
|
382 cons]
|
adamc@156
|
383 end
|
adamc@156
|
384
|
adamc@31
|
385 fun p_sgn_item env (sgi, _) =
|
adamc@31
|
386 case sgi of
|
adamc@31
|
387 SgiConAbs (x, n, k) => box [string "con",
|
adamc@31
|
388 space,
|
adamc@31
|
389 p_named x n,
|
adamc@31
|
390 space,
|
adamc@31
|
391 string "::",
|
adamc@31
|
392 space,
|
adamc@31
|
393 p_kind k]
|
adamc@31
|
394 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@31
|
395 space,
|
adamc@31
|
396 p_named x n,
|
adamc@31
|
397 space,
|
adamc@31
|
398 string "::",
|
adamc@31
|
399 space,
|
adamc@31
|
400 p_kind k,
|
adamc@31
|
401 space,
|
adamc@31
|
402 string "=",
|
adamc@31
|
403 space,
|
adamc@31
|
404 p_con env c]
|
adamc@156
|
405 | SgiDatatype x => p_datatype env x
|
adamc@162
|
406 | SgiDatatypeImp (x, _, m1, ms, x', _) =>
|
adamc@156
|
407 let
|
adamc@156
|
408 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@156
|
409 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@156
|
410 in
|
adamc@156
|
411 box [string "datatype",
|
adamc@156
|
412 space,
|
adamc@156
|
413 string x,
|
adamc@156
|
414 space,
|
adamc@156
|
415 string "=",
|
adamc@156
|
416 space,
|
adamc@156
|
417 string "datatype",
|
adamc@156
|
418 space,
|
adamc@156
|
419 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@156
|
420 end
|
adamc@31
|
421 | SgiVal (x, n, c) => box [string "val",
|
adamc@31
|
422 space,
|
adamc@31
|
423 p_named x n,
|
adamc@31
|
424 space,
|
adamc@31
|
425 string ":",
|
adamc@31
|
426 space,
|
adamc@31
|
427 p_con env c]
|
adamc@31
|
428 | SgiStr (x, n, sgn) => box [string "structure",
|
adamc@31
|
429 space,
|
adamc@31
|
430 p_named x n,
|
adamc@31
|
431 space,
|
adamc@31
|
432 string ":",
|
adamc@31
|
433 space,
|
adamc@31
|
434 p_sgn env sgn]
|
adamc@59
|
435 | SgiSgn (x, n, sgn) => box [string "signature",
|
adamc@59
|
436 space,
|
adamc@59
|
437 p_named x n,
|
adamc@59
|
438 space,
|
adamc@59
|
439 string "=",
|
adamc@59
|
440 space,
|
adamc@59
|
441 p_sgn env sgn]
|
adamc@88
|
442 | SgiConstraint (c1, c2) => box [string "constraint",
|
adamc@88
|
443 space,
|
adamc@88
|
444 p_con env c1,
|
adamc@88
|
445 space,
|
adamc@88
|
446 string "~",
|
adamc@88
|
447 space,
|
adamc@88
|
448 p_con env c2]
|
adamc@31
|
449
|
adamc@31
|
450 and p_sgn env (sgn, _) =
|
adamc@31
|
451 case sgn of
|
adamc@31
|
452 SgnConst sgis => box [string "sig",
|
adamc@31
|
453 newline,
|
adamc@32
|
454 let
|
adamc@32
|
455 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
|
adamc@32
|
456 (p_sgn_item env sgi,
|
adamc@32
|
457 E.sgiBinds env sgi))
|
adamc@32
|
458 env sgis
|
adamc@32
|
459 in
|
adamc@32
|
460 p_list_sep newline (fn x => x) psgis
|
adamc@32
|
461 end,
|
adamc@31
|
462 newline,
|
adamc@31
|
463 string "end"]
|
adamc@88
|
464 | SgnVar n => ((string (#1 (E.lookupSgnNamed env n)))
|
adamc@88
|
465 handle E.UnboundNamed _ => string ("UNBOUND_SGN_" ^ Int.toString n))
|
adamc@41
|
466 | SgnFun (x, n, sgn, sgn') => box [string "functor",
|
adamc@41
|
467 space,
|
adamc@41
|
468 string "(",
|
adamc@41
|
469 string x,
|
adamc@41
|
470 space,
|
adamc@41
|
471 string ":",
|
adamc@41
|
472 space,
|
adamc@41
|
473 p_sgn env sgn,
|
adamc@41
|
474 string ")",
|
adamc@41
|
475 space,
|
adamc@41
|
476 string ":",
|
adamc@41
|
477 space,
|
adamc@41
|
478 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
|
adamc@42
|
479 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
|
adamc@42
|
480 space,
|
adamc@42
|
481 string "where",
|
adamc@42
|
482 space,
|
adamc@42
|
483 string "con",
|
adamc@42
|
484 space,
|
adamc@42
|
485 string x,
|
adamc@42
|
486 space,
|
adamc@42
|
487 string "=",
|
adamc@42
|
488 space,
|
adamc@42
|
489 p_con env c]
|
adamc@59
|
490 | SgnProj (m1, ms, x) =>
|
adamc@59
|
491 let
|
adamc@88
|
492 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@88
|
493 handle E.UnboundNamed _ => "UNBOUND_SGN_" ^ Int.toString m1
|
adamc@88
|
494
|
adamc@59
|
495 val m1s = if !debug then
|
adamc@59
|
496 m1x ^ "__" ^ Int.toString m1
|
adamc@59
|
497 else
|
adamc@59
|
498 m1x
|
adamc@88
|
499 in
|
adamc@59
|
500 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@59
|
501 end
|
adamc@31
|
502 | SgnError => string "<ERROR>"
|
adamc@31
|
503
|
adamc@123
|
504 fun p_vali env (x, n, t, e) = box [p_named x n,
|
adamc@123
|
505 space,
|
adamc@123
|
506 string ":",
|
adamc@123
|
507 space,
|
adamc@123
|
508 p_con env t,
|
adamc@123
|
509 space,
|
adamc@123
|
510 string "=",
|
adamc@123
|
511 space,
|
adamc@123
|
512 p_exp env e]
|
adamc@123
|
513
|
adamc@123
|
514 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@3
|
515 case d of
|
adamc@31
|
516 DCon (x, n, k, c) => box [string "con",
|
adamc@31
|
517 space,
|
adamc@31
|
518 p_named x n,
|
adamc@31
|
519 space,
|
adamc@31
|
520 string "::",
|
adamc@31
|
521 space,
|
adamc@31
|
522 p_kind k,
|
adamc@31
|
523 space,
|
adamc@31
|
524 string "=",
|
adamc@31
|
525 space,
|
adamc@31
|
526 p_con env c]
|
adamc@156
|
527 | DDatatype x => p_datatype env x
|
adamc@162
|
528 | DDatatypeImp (x, _, m1, ms, x', _) =>
|
adamc@156
|
529 let
|
adamc@156
|
530 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@156
|
531 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@156
|
532 in
|
adamc@156
|
533 box [string "datatype",
|
adamc@156
|
534 space,
|
adamc@156
|
535 string x,
|
adamc@156
|
536 space,
|
adamc@156
|
537 string "=",
|
adamc@156
|
538 space,
|
adamc@156
|
539 string "datatype",
|
adamc@156
|
540 space,
|
adamc@156
|
541 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@156
|
542 end
|
adamc@123
|
543 | DVal vi => box [string "val",
|
adamc@123
|
544 space,
|
adamc@123
|
545 p_vali env vi]
|
adamc@123
|
546 | DValRec vis =>
|
adamc@123
|
547 let
|
adamc@123
|
548 val env = E.declBinds env dAll
|
adamc@123
|
549 in
|
adamc@123
|
550 box [string "val",
|
adamc@123
|
551 space,
|
adamc@123
|
552 string "rec",
|
adamc@123
|
553 space,
|
adamc@123
|
554 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
|
adamc@123
|
555 end
|
adamc@31
|
556
|
adamc@31
|
557 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@31
|
558 space,
|
adamc@31
|
559 p_named x n,
|
adamc@31
|
560 space,
|
adamc@31
|
561 string "=",
|
adamc@31
|
562 space,
|
adamc@31
|
563 p_sgn env sgn]
|
adamc@31
|
564 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@31
|
565 space,
|
adamc@31
|
566 p_named x n,
|
adamc@31
|
567 space,
|
adamc@31
|
568 string ":",
|
adamc@31
|
569 space,
|
adamc@31
|
570 p_sgn env sgn,
|
adamc@31
|
571 space,
|
adamc@31
|
572 string "=",
|
adamc@31
|
573 space,
|
adamc@31
|
574 p_str env str]
|
adamc@48
|
575 | DFfiStr (x, n, sgn) => box [string "extern",
|
adamc@48
|
576 space,
|
adamc@48
|
577 string "structure",
|
adamc@48
|
578 space,
|
adamc@48
|
579 p_named x n,
|
adamc@48
|
580 space,
|
adamc@48
|
581 string ":",
|
adamc@48
|
582 space,
|
adamc@48
|
583 p_sgn env sgn]
|
adamc@88
|
584 | DConstraint (c1, c2) => box [string "constraint",
|
adamc@88
|
585 space,
|
adamc@88
|
586 p_con env c1,
|
adamc@88
|
587 space,
|
adamc@88
|
588 string "~",
|
adamc@88
|
589 space,
|
adamc@88
|
590 p_con env c2]
|
adamc@109
|
591 | DExport (_, sgn, str) => box [string "export",
|
adamc@110
|
592 space,
|
adamc@109
|
593 p_str env str,
|
adamc@109
|
594 space,
|
adamc@109
|
595 string ":",
|
adamc@109
|
596 space,
|
adamc@109
|
597 p_sgn env sgn]
|
adamc@31
|
598
|
adamc@31
|
599 and p_str env (str, _) =
|
adamc@31
|
600 case str of
|
adamc@31
|
601 StrConst ds => box [string "struct",
|
adamc@31
|
602 newline,
|
adamc@32
|
603 p_file env ds,
|
adamc@31
|
604 newline,
|
adamc@31
|
605 string "end"]
|
adamc@88
|
606 | StrVar n => ((string (#1 (E.lookupStrNamed env n)))
|
adamc@88
|
607 handle E.UnboundNamed _ => string ("UNBOUND_STR_" ^ Int.toString n))
|
adamc@34
|
608 | StrProj (str, s) => box [p_str env str,
|
adamc@34
|
609 string ".",
|
adamc@34
|
610 string s]
|
adamc@41
|
611 | StrFun (x, n, sgn, sgn', str) =>
|
adamc@41
|
612 let
|
adamc@41
|
613 val env' = E.pushStrNamedAs env x n sgn
|
adamc@41
|
614 in
|
adamc@41
|
615 box [string "functor",
|
adamc@41
|
616 space,
|
adamc@41
|
617 string "(",
|
adamc@41
|
618 string x,
|
adamc@41
|
619 space,
|
adamc@41
|
620 string ":",
|
adamc@41
|
621 space,
|
adamc@41
|
622 p_sgn env sgn,
|
adamc@41
|
623 string ")",
|
adamc@41
|
624 space,
|
adamc@41
|
625 string ":",
|
adamc@41
|
626 space,
|
adamc@41
|
627 p_sgn env' sgn',
|
adamc@41
|
628 space,
|
adamc@41
|
629 string "=>",
|
adamc@41
|
630 space,
|
adamc@41
|
631 p_str env' str]
|
adamc@41
|
632 end
|
adamc@44
|
633 | StrApp (str1, str2) => box [p_str env str1,
|
adamc@44
|
634 string "(",
|
adamc@44
|
635 p_str env str2,
|
adamc@44
|
636 string ")"]
|
adamc@31
|
637 | StrError => string "<ERROR>"
|
adamc@3
|
638
|
adamc@32
|
639 and p_file env file =
|
adamc@3
|
640 let
|
adamc@31
|
641 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
642 (p_decl env d,
|
adamc@31
|
643 E.declBinds env d))
|
adamc@31
|
644 env file
|
adamc@3
|
645 in
|
adamc@3
|
646 p_list_sep newline (fn x => x) pds
|
adamc@3
|
647 end
|
adamc@3
|
648
|
adamc@3
|
649 end
|