annotate src/elab_print.sml @ 207:cc68da3801bc

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