annotate src/elab_print.sml @ 1720:30c0ca20bf59

Pretty-print tuple types using tuple syntax
author Adam Chlipala <adam@chlipala.net>
date Sat, 21 Apr 2012 15:09:02 -0400
parents 05ae68e019b6
children 5df85275c0d4
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@244 28 (* Pretty-printing elaborated Ur/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@623 41 fun p_kind' par env (k, _) =
adamc@3 42 case k of
adamc@3 43 KType => string "Type"
adamc@623 44 | KArrow (k1, k2) => parenIf par (box [p_kind' true env k1,
adamc@3 45 space,
adamc@3 46 string "->",
adamc@3 47 space,
adamc@623 48 p_kind env k2])
adamc@3 49 | KName => string "Name"
adamc@623 50 | KRecord k => box [string "{", p_kind env k, string "}"]
adamc@82 51 | KUnit => string "Unit"
adamc@207 52 | KTuple ks => box [string "(",
adamc@623 53 p_list_sep (box [space, string "*", space]) (p_kind env) ks,
adamc@207 54 string ")"]
adamc@3 55
adamc@3 56 | KError => string "<ERROR>"
adam@1639 57 | KUnif (_, _, ref (KKnown k)) => p_kind' par env k
adamc@76 58 | KUnif (_, s, _) => string ("<UNIF:" ^ s ^ ">")
adam@1639 59 | KTupleUnif (_, _, ref (KKnown k)) => p_kind' par env k
adam@1302 60 | KTupleUnif (_, nks, _) => box [string "(",
adam@1302 61 p_list_sep (box [space, string "*", space])
adam@1302 62 (fn (n, k) => box [string (Int.toString n ^ ":"),
adam@1302 63 space,
adam@1302 64 p_kind env k]) nks,
adam@1302 65 space,
adam@1302 66 string "*",
adam@1302 67 space,
adam@1302 68 string "...)"]
adamc@3 69
adamc@623 70 | KRel n => ((if !debug then
adamc@623 71 string (E.lookupKRel env n ^ "_" ^ Int.toString n)
adamc@623 72 else
adamc@623 73 string (E.lookupKRel env n))
adamc@623 74 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@623 75 | KFun (x, k) => box [string x,
adamc@623 76 space,
adamc@623 77 string "-->",
adamc@623 78 space,
adamc@623 79 p_kind (E.pushKRel env x) k]
adamc@623 80
adamc@624 81 and p_kind env = p_kind' false env
adamc@3 82
adamc@3 83 fun p_explicitness e =
adamc@3 84 case e of
adamc@3 85 Explicit => string "::"
adamc@3 86 | Implicit => string ":::"
adamc@3 87
adamc@3 88 fun p_con' par env (c, _) =
adamc@3 89 case c of
adamc@623 90 TFun (t1, t2) => parenIf true (box [p_con' true env t1,
adamc@3 91 space,
adamc@3 92 string "->",
adamc@3 93 space,
adamc@3 94 p_con env t2])
adamc@3 95 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@3 96 space,
adamc@3 97 p_explicitness e,
adamc@3 98 space,
adamc@623 99 p_kind env k,
adamc@3 100 space,
adamc@3 101 string "->",
adamc@3 102 space,
adamc@3 103 p_con (E.pushCRel env x k) c])
adamc@628 104 | TDisjoint (c1, c2, c3) => parenIf par (box [string "[",
adamc@628 105 p_con env c1,
adamc@628 106 space,
adamc@628 107 string "~",
adamc@628 108 space,
adamc@628 109 p_con env c2,
adamc@628 110 string "]",
adamc@628 111 space,
adamc@628 112 string "=>",
adamc@628 113 space,
adamc@628 114 p_con env c3])
adam@1720 115 | TRecord (CRecord (_, xcs), _) =>
adam@1720 116 let
adam@1720 117 fun isTuple (n, xcs) =
adam@1720 118 case xcs of
adam@1720 119 [] => n > 2
adam@1720 120 | ((CName s, _), _) :: xcs' =>
adam@1720 121 s = Int.toString n andalso isTuple (n+1, xcs')
adam@1720 122 | _ => false
adam@1720 123 in
adam@1720 124 if isTuple (1, xcs) then
adam@1720 125 case xcs of
adam@1720 126 (_, c) :: xcs =>
adam@1720 127 parenIf par (box [p_con' true env c,
adam@1720 128 p_list_sep (box []) (fn (_, c) => box [space,
adam@1720 129 string "*",
adam@1720 130 space,
adam@1720 131 p_con' true env c]) xcs])
adam@1720 132 | _ => raise Fail "ElabPrint: surprise empty tuple"
adam@1720 133 else
adam@1720 134 box [string "{",
adam@1720 135 p_list (fn (x, c) =>
adam@1720 136 box [p_name env x,
adam@1720 137 space,
adam@1720 138 string ":",
adam@1720 139 space,
adam@1720 140 p_con env c]) xcs,
adam@1720 141 string "}"]
adam@1720 142 end
adamc@3 143 | TRecord c => box [string "$",
adamc@3 144 p_con' true env c]
adamc@3 145
adamc@11 146 | CRel n =>
adamc@71 147 ((if !debug then
adamc@71 148 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@71 149 else
adamc@71 150 string (#1 (E.lookupCRel env n)))
adamc@71 151 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 152 | CNamed n =>
adamc@34 153 ((if !debug then
adamc@34 154 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@34 155 else
adamc@34 156 string (#1 (E.lookupCNamed env n)))
adamc@34 157 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 158 | CModProj (m1, ms, x) =>
adamc@34 159 let
adamc@88 160 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 161 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@88 162
adamc@34 163 val m1s = if !debug then
adamc@34 164 m1x ^ "__" ^ Int.toString m1
adamc@34 165 else
adamc@34 166 m1x
adamc@34 167 in
adamc@34 168 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@88 169 end
adamc@3 170
adamc@3 171 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@3 172 space,
adamc@3 173 p_con' true env c2])
adamc@147 174 | CAbs (x, k, c) => parenIf true (box [string "fn",
adamc@147 175 space,
adamc@147 176 string x,
adamc@147 177 space,
adamc@147 178 string "::",
adamc@147 179 space,
adamc@623 180 p_kind env k,
adamc@147 181 space,
adamc@147 182 string "=>",
adamc@147 183 space,
adamc@147 184 p_con (E.pushCRel env x k) c])
adamc@3 185
adamc@3 186 | CName s => box [string "#", string s]
adamc@3 187
adamc@12 188 | CRecord (k, xcs) =>
adamc@12 189 if !debug then
adamc@12 190 parenIf par (box [string "[",
adamc@12 191 p_list (fn (x, c) =>
adam@1716 192 box [p_name env x,
adamc@12 193 space,
adamc@12 194 string "=",
adamc@12 195 space,
adamc@12 196 p_con env c]) xcs,
adamc@12 197 string "]::",
adamc@623 198 p_kind env k])
adamc@12 199 else
adamc@12 200 parenIf par (box [string "[",
adamc@12 201 p_list (fn (x, c) =>
adam@1716 202 box [p_name env x,
adamc@12 203 space,
adamc@12 204 string "=",
adamc@12 205 space,
adamc@12 206 p_con env c]) xcs,
adamc@12 207 string "]"])
adamc@3 208 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@3 209 space,
adamc@3 210 string "++",
adamc@3 211 space,
adamc@3 212 p_con env c2])
adamc@621 213 | CMap _ => string "map"
adamc@3 214
adamc@82 215 | CUnit => string "()"
adamc@82 216
adamc@207 217 | CTuple cs => box [string "(",
adamc@207 218 p_list (p_con env) cs,
adamc@207 219 string ")"]
adamc@207 220 | CProj (c, n) => box [p_con env c,
adamc@207 221 string ".",
adamc@207 222 string (Int.toString n)]
adamc@207 223
adamc@3 224 | CError => string "<ERROR>"
adam@1639 225 | CUnif (nl, _, _, _, ref (Known c)) => p_con' par env (E.mliftConInCon nl c)
adam@1303 226 | CUnif (nl, _, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
adam@1303 227 p_kind env k,
adam@1303 228 case nl of
adam@1303 229 0 => box []
adam@1303 230 | _ => string ("+" ^ Int.toString nl),
adam@1303 231 string ">"]
adamc@623 232
adamc@623 233 | CKAbs (x, c) => box [string x,
adamc@623 234 space,
adamc@623 235 string "==>",
adamc@623 236 space,
adamc@623 237 p_con (E.pushKRel env x) c]
adamc@623 238 | CKApp (c, k) => box [p_con env c,
adamc@623 239 string "[[",
adamc@623 240 p_kind env k,
adamc@623 241 string "]]"]
adamc@623 242 | TKFun (x, c) => box [string x,
adamc@623 243 space,
adamc@623 244 string "-->",
adamc@623 245 space,
adamc@623 246 p_con (E.pushKRel env x) c]
adamc@623 247
adamc@3 248
adamc@3 249 and p_con env = p_con' false env
adamc@3 250
adamc@20 251 and p_name env (all as (c, _)) =
adamc@20 252 case c of
adamc@20 253 CName s => string s
adamc@20 254 | _ => p_con env all
adamc@20 255
adamc@171 256 fun p_patCon env pc =
adamc@171 257 case pc of
adamc@171 258 PConVar n =>
adamc@171 259 ((if !debug then
adamc@171 260 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@171 261 else
adamc@171 262 string (#1 (E.lookupENamed env n)))
adamc@448 263 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@171 264 | PConProj (m1, ms, x) =>
adamc@171 265 let
adamc@171 266 val m1x = #1 (E.lookupStrNamed env m1)
adamc@171 267 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@171 268
adamc@171 269 val m1s = if !debug then
adamc@171 270 m1x ^ "__" ^ Int.toString m1
adamc@171 271 else
adamc@171 272 m1x
adamc@171 273 in
adamc@171 274 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@171 275 end
adamc@171 276
adamc@171 277 fun p_pat' par env (p, _) =
adamc@171 278 case p of
adamc@171 279 PWild => string "_"
adamc@182 280 | PVar (s, _) => string s
adamc@173 281 | PPrim p => Prim.p_t p
adamc@191 282 | PCon (_, pc, _, NONE) => p_patCon env pc
adamc@191 283 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
adamc@188 284 space,
adamc@188 285 p_pat' true env p])
adamc@176 286 | PRecord xps =>
adamc@176 287 box [string "{",
adamc@1272 288 p_list_sep (box [string ",", space]) (fn (x, p, t) =>
adamc@176 289 box [string x,
adamc@176 290 space,
adamc@176 291 string "=",
adamc@176 292 space,
adamc@1272 293 p_pat env p,
adamc@1272 294 if !debug then
adamc@1272 295 box [space,
adamc@1272 296 string ":",
adamc@1272 297 space,
adamc@1272 298 p_con env t]
adamc@1272 299 else
adamc@1272 300 box []]) xps,
adamc@176 301 string "}"]
adamc@171 302
adamc@175 303 and p_pat x = p_pat' false x
adamc@171 304
adamc@9 305 fun p_exp' par env (e, _) =
adamc@9 306 case e of
adamc@14 307 EPrim p => Prim.p_t p
adamc@14 308 | ERel n =>
adamc@88 309 ((if !debug then
adamc@88 310 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@88 311 else
adamc@88 312 string (#1 (E.lookupERel env n)))
adamc@88 313 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 314 | ENamed n =>
adamc@88 315 ((if !debug then
adamc@88 316 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@88 317 else
adamc@88 318 string (#1 (E.lookupENamed env n)))
adamc@448 319 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 320 | EModProj (m1, ms, x) =>
adamc@34 321 let
adamc@88 322 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 323 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@88 324
adamc@34 325 val m1s = if !debug then
adamc@34 326 m1x ^ "__" ^ Int.toString m1
adamc@34 327 else
adamc@34 328 m1x
adamc@34 329 in
adamc@34 330 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@34 331 end
adamc@34 332
adamc@9 333 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@9 334 space,
adamc@9 335 p_exp' true env e2])
adamc@26 336 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@26 337 space,
adamc@26 338 string x,
adamc@26 339 space,
adamc@26 340 string ":",
adamc@26 341 space,
adamc@26 342 p_con env t,
adamc@26 343 space,
adamc@26 344 string "=>",
adamc@26 345 space,
adamc@26 346 p_exp (E.pushERel env x t) e])
adamc@9 347 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@9 348 space,
adamc@9 349 string "[",
adamc@9 350 p_con env c,
adamc@9 351 string "]"])
adamc@9 352 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@9 353 space,
adamc@9 354 string x,
adamc@9 355 space,
adamc@9 356 p_explicitness exp,
adamc@9 357 space,
adamc@623 358 p_kind env k,
adamc@9 359 space,
adamc@9 360 string "=>",
adamc@9 361 space,
adamc@9 362 p_exp (E.pushCRel env x k) e])
adamc@9 363
adamc@12 364 | ERecord xes => box [string "{",
adamc@29 365 p_list (fn (x, e, _) =>
adamc@21 366 box [p_name env x,
adamc@12 367 space,
adamc@12 368 string "=",
adamc@12 369 space,
adamc@12 370 p_exp env e]) xes,
adamc@12 371 string "}"]
adamc@12 372 | EField (e, c, {field, rest}) =>
adamc@12 373 if !debug then
adamc@12 374 box [p_exp' true env e,
adamc@12 375 string ".",
adamc@12 376 p_con' true env c,
adamc@12 377 space,
adamc@12 378 string "[",
adamc@12 379 p_con env field,
adamc@12 380 space,
adamc@12 381 string " in ",
adamc@12 382 space,
adamc@12 383 p_con env rest,
adamc@12 384 string "]"]
adamc@12 385 else
adamc@12 386 box [p_exp' true env e,
adamc@12 387 string ".",
adamc@12 388 p_con' true env c]
adamc@445 389 | EConcat (e1, c1, e2, c2) =>
adamc@339 390 parenIf par (if !debug then
adamc@445 391 box [p_exp' true env e1,
adamc@445 392 space,
adamc@445 393 string ":",
adamc@445 394 space,
adamc@445 395 p_con env c1,
adamc@445 396 space,
adamc@445 397 string "++",
adamc@445 398 space,
adamc@445 399 p_exp' true env e2,
adamc@445 400 space,
adamc@445 401 string ":",
adamc@445 402 space,
adamc@445 403 p_con env c2]
adamc@445 404 else
adamc@445 405 box [p_exp' true env e1,
adamc@339 406 space,
adamc@494 407 string "++",
adamc@339 408 space,
adamc@339 409 p_exp' true env e2])
adamc@149 410 | ECut (e, c, {field, rest}) =>
adamc@149 411 parenIf par (if !debug then
adamc@149 412 box [p_exp' true env e,
adamc@149 413 space,
adamc@149 414 string "--",
adamc@149 415 space,
adamc@149 416 p_con' true env c,
adamc@149 417 space,
adamc@149 418 string "[",
adamc@149 419 p_con env field,
adamc@149 420 space,
adamc@149 421 string " in ",
adamc@149 422 space,
adamc@149 423 p_con env rest,
adamc@149 424 string "]"]
adamc@149 425 else
adamc@149 426 box [p_exp' true env e,
adamc@149 427 space,
adamc@149 428 string "--",
adamc@149 429 space,
adamc@149 430 p_con' true env c])
adamc@493 431 | ECutMulti (e, c, {rest}) =>
adamc@493 432 parenIf par (if !debug then
adamc@493 433 box [p_exp' true env e,
adamc@493 434 space,
adamc@493 435 string "---",
adamc@493 436 space,
adamc@493 437 p_con' true env c,
adamc@493 438 space,
adamc@493 439 string "[",
adamc@493 440 p_con env rest,
adamc@493 441 string "]"]
adamc@493 442 else
adamc@493 443 box [p_exp' true env e,
adamc@493 444 space,
adamc@493 445 string "---",
adamc@493 446 space,
adamc@493 447 p_con' true env c])
adamc@493 448
adamc@171 449 | ECase (e, pes, _) => parenIf par (box [string "case",
adamc@171 450 space,
adamc@171 451 p_exp env e,
adamc@171 452 space,
adamc@171 453 string "of",
adamc@171 454 space,
adamc@171 455 p_list_sep (box [space, string "|", space])
adamc@171 456 (fn (p, e) => box [p_pat env p,
adamc@171 457 space,
adamc@171 458 string "=>",
adamc@171 459 space,
adamc@243 460 p_exp (E.patBinds env p) e]) pes])
adamc@171 461
adamc@9 462 | EError => string "<ERROR>"
adamc@228 463 | EUnif (ref (SOME e)) => p_exp env e
adamc@228 464 | EUnif _ => string "_"
adamc@9 465
adamc@825 466 | ELet (ds, e, _) =>
adamc@447 467 let
adamc@447 468 val (dsp, env) = ListUtil.foldlMap
adamc@447 469 (fn (d, env) =>
adamc@447 470 (p_edecl env d,
adamc@447 471 E.edeclBinds env d))
adamc@447 472 env ds
adamc@447 473 in
adamc@447 474 box [string "let",
adamc@447 475 newline,
adamc@447 476 box [p_list_sep newline (fn x => x) dsp],
adamc@447 477 newline,
adamc@447 478 string "in",
adamc@447 479 newline,
adamc@447 480 box [p_exp env e],
adamc@447 481 newline,
adamc@447 482 string "end"]
adamc@447 483 end
adamc@447 484
adamc@623 485 | EKAbs (x, e) => box [string x,
adamc@623 486 space,
adamc@623 487 string "==>",
adamc@623 488 space,
adamc@623 489 p_exp (E.pushKRel env x) e]
adamc@623 490 | EKApp (e, k) => box [p_exp env e,
adamc@623 491 string "[[",
adamc@623 492 p_kind env k,
adamc@623 493 string "]]"]
adamc@623 494
adamc@9 495 and p_exp env = p_exp' false env
adamc@9 496
adamc@447 497 and p_edecl env (dAll as (d, _)) =
adamc@447 498 case d of
adamc@825 499 EDVal (p, t, e) => box [string "val",
adamc@825 500 space,
adamc@825 501 p_pat env p,
adamc@825 502 space,
adamc@825 503 string ":",
adamc@825 504 space,
adamc@825 505 p_con env t,
adamc@825 506 space,
adamc@825 507 string "=",
adamc@825 508 space,
adamc@825 509 p_exp env e]
adamc@447 510 | EDValRec vis =>
adamc@447 511 let
adamc@447 512 val env = E.edeclBinds env dAll
adamc@447 513 in
adamc@447 514 box [string "val",
adamc@447 515 space,
adamc@447 516 string "rec",
adamc@447 517 space,
adamc@447 518 p_list_sep (box [newline, string "and", space]) (p_evali env) vis]
adamc@447 519 end
adamc@447 520
adamc@447 521 and p_evali env (x, t, e) = box [string x,
adamc@447 522 space,
adamc@447 523 string ":",
adamc@447 524 space,
adamc@447 525 p_con env t,
adamc@447 526 space,
adamc@447 527 string "=",
adamc@447 528 space,
adamc@447 529 p_exp env e]
adamc@31 530
adamc@191 531 fun p_datatype env (x, n, xs, cons) =
adamc@156 532 let
adamc@191 533 val k = (KType, ErrorMsg.dummySpan)
adamc@191 534 val env = E.pushCNamedAs env x n k NONE
adamc@191 535 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
adamc@156 536 in
adamc@805 537 box [string x,
adamc@191 538 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@156 539 space,
adamc@156 540 string "=",
adamc@156 541 space,
adamc@156 542 p_list_sep (box [space, string "|", space])
adamc@156 543 (fn (x, _, NONE) => string x
adamc@156 544 | (x, _, SOME t) => box [string x, space, string "of", space, p_con env t])
adamc@156 545 cons]
adamc@156 546 end
adamc@156 547
adamc@447 548 fun p_named x n =
adamc@447 549 if !debug then
adamc@447 550 box [string x,
adamc@447 551 string "__",
adamc@447 552 string (Int.toString n)]
adamc@447 553 else
adamc@447 554 string x
adamc@447 555
adamc@805 556 fun p_sgn_item env (sgiAll as (sgi, _)) =
adamc@31 557 case sgi of
adamc@31 558 SgiConAbs (x, n, k) => box [string "con",
adamc@31 559 space,
adamc@31 560 p_named x n,
adamc@31 561 space,
adamc@31 562 string "::",
adamc@31 563 space,
adamc@623 564 p_kind env k]
adamc@31 565 | SgiCon (x, n, k, c) => box [string "con",
adamc@31 566 space,
adamc@31 567 p_named x n,
adamc@31 568 space,
adamc@31 569 string "::",
adamc@31 570 space,
adamc@623 571 p_kind env k,
adamc@31 572 space,
adamc@31 573 string "=",
adamc@31 574 space,
adamc@31 575 p_con env c]
adamc@805 576 | SgiDatatype x => box [string "datatype",
adamc@805 577 space,
adamc@805 578 p_list_sep (box [space, string "and", space]) (p_datatype (E.sgiBinds env sgiAll)) x]
adamc@191 579 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@156 580 let
adamc@156 581 val m1x = #1 (E.lookupStrNamed env m1)
adamc@156 582 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@156 583 in
adamc@156 584 box [string "datatype",
adamc@156 585 space,
adamc@156 586 string x,
adamc@156 587 space,
adamc@156 588 string "=",
adamc@156 589 space,
adamc@156 590 string "datatype",
adamc@156 591 space,
adamc@156 592 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@156 593 end
adamc@31 594 | SgiVal (x, n, c) => box [string "val",
adamc@31 595 space,
adamc@31 596 p_named x n,
adamc@31 597 space,
adamc@31 598 string ":",
adamc@31 599 space,
adamc@31 600 p_con env c]
adamc@31 601 | SgiStr (x, n, sgn) => box [string "structure",
adamc@31 602 space,
adamc@31 603 p_named x n,
adamc@31 604 space,
adamc@31 605 string ":",
adamc@31 606 space,
adamc@31 607 p_sgn env sgn]
adamc@59 608 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@59 609 space,
adamc@59 610 p_named x n,
adamc@59 611 space,
adamc@59 612 string "=",
adamc@59 613 space,
adamc@59 614 p_sgn env sgn]
adamc@88 615 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 616 space,
adamc@88 617 p_con env c1,
adamc@88 618 space,
adamc@88 619 string "~",
adamc@88 620 space,
adamc@88 621 p_con env c2]
adamc@563 622 | SgiClassAbs (x, n, k) => box [string "class",
adamc@563 623 space,
adamc@563 624 p_named x n,
adamc@563 625 space,
adamc@563 626 string "::",
adamc@563 627 space,
adamc@623 628 p_kind env k]
adamc@563 629 | SgiClass (x, n, k, c) => box [string "class",
adamc@563 630 space,
adamc@563 631 p_named x n,
adamc@563 632 space,
adamc@563 633 string "::",
adamc@563 634 space,
adamc@623 635 p_kind env k,
adamc@563 636 space,
adamc@563 637 string "=",
adamc@563 638 space,
adamc@563 639 p_con env c]
adamc@31 640
adamc@31 641 and p_sgn env (sgn, _) =
adamc@31 642 case sgn of
adamc@31 643 SgnConst sgis => box [string "sig",
adamc@31 644 newline,
adamc@32 645 let
adamc@32 646 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@32 647 (p_sgn_item env sgi,
adamc@32 648 E.sgiBinds env sgi))
adamc@32 649 env sgis
adamc@32 650 in
adamc@32 651 p_list_sep newline (fn x => x) psgis
adamc@32 652 end,
adamc@31 653 newline,
adamc@31 654 string "end"]
adamc@88 655 | SgnVar n => ((string (#1 (E.lookupSgnNamed env n)))
adamc@88 656 handle E.UnboundNamed _ => string ("UNBOUND_SGN_" ^ Int.toString n))
adamc@41 657 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@41 658 space,
adamc@41 659 string "(",
adamc@41 660 string x,
adamc@41 661 space,
adamc@41 662 string ":",
adamc@41 663 space,
adamc@41 664 p_sgn env sgn,
adamc@41 665 string ")",
adamc@41 666 space,
adamc@41 667 string ":",
adamc@41 668 space,
adamc@41 669 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
adamc@42 670 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@42 671 space,
adamc@42 672 string "where",
adamc@42 673 space,
adamc@42 674 string "con",
adamc@42 675 space,
adamc@42 676 string x,
adamc@42 677 space,
adamc@42 678 string "=",
adamc@42 679 space,
adamc@42 680 p_con env c]
adamc@59 681 | SgnProj (m1, ms, x) =>
adamc@59 682 let
adamc@88 683 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 684 handle E.UnboundNamed _ => "UNBOUND_SGN_" ^ Int.toString m1
adamc@88 685
adamc@59 686 val m1s = if !debug then
adamc@59 687 m1x ^ "__" ^ Int.toString m1
adamc@59 688 else
adamc@59 689 m1x
adamc@88 690 in
adamc@59 691 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@59 692 end
adamc@31 693 | SgnError => string "<ERROR>"
adamc@31 694
adamc@123 695 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@123 696 space,
adamc@123 697 string ":",
adamc@123 698 space,
adamc@123 699 p_con env t,
adamc@123 700 space,
adamc@123 701 string "=",
adamc@123 702 space,
adamc@123 703 p_exp env e]
adamc@123 704
adamc@447 705
adamc@447 706
adamc@123 707 fun p_decl env (dAll as (d, _) : decl) =
adamc@3 708 case d of
adamc@31 709 DCon (x, n, k, c) => box [string "con",
adamc@31 710 space,
adamc@31 711 p_named x n,
adamc@31 712 space,
adamc@31 713 string "::",
adamc@31 714 space,
adamc@623 715 p_kind env k,
adamc@31 716 space,
adamc@31 717 string "=",
adamc@31 718 space,
adamc@31 719 p_con env c]
adamc@805 720 | DDatatype x => box [string "datatype",
adamc@805 721 space,
adamc@805 722 p_list_sep (box [space, string "and", space]) (p_datatype (E.declBinds env dAll)) x]
adamc@191 723 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@156 724 let
adamc@156 725 val m1x = #1 (E.lookupStrNamed env m1)
adamc@156 726 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@156 727 in
adamc@156 728 box [string "datatype",
adamc@156 729 space,
adamc@156 730 string x,
adamc@156 731 space,
adamc@156 732 string "=",
adamc@156 733 space,
adamc@156 734 string "datatype",
adamc@156 735 space,
adamc@156 736 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@156 737 end
adamc@123 738 | DVal vi => box [string "val",
adamc@123 739 space,
adamc@123 740 p_vali env vi]
adamc@123 741 | DValRec vis =>
adamc@123 742 let
adamc@123 743 val env = E.declBinds env dAll
adamc@123 744 in
adamc@123 745 box [string "val",
adamc@123 746 space,
adamc@123 747 string "rec",
adamc@123 748 space,
adamc@123 749 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@123 750 end
adamc@31 751
adamc@31 752 | DSgn (x, n, sgn) => box [string "signature",
adamc@31 753 space,
adamc@31 754 p_named x n,
adamc@31 755 space,
adamc@31 756 string "=",
adamc@31 757 space,
adamc@31 758 p_sgn env sgn]
adamc@31 759 | DStr (x, n, sgn, str) => box [string "structure",
adamc@31 760 space,
adamc@31 761 p_named x n,
adamc@31 762 space,
adamc@31 763 string ":",
adamc@31 764 space,
adamc@31 765 p_sgn env sgn,
adamc@31 766 space,
adamc@31 767 string "=",
adamc@31 768 space,
adamc@31 769 p_str env str]
adamc@48 770 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 771 space,
adamc@48 772 string "structure",
adamc@48 773 space,
adamc@48 774 p_named x n,
adamc@48 775 space,
adamc@48 776 string ":",
adamc@48 777 space,
adamc@48 778 p_sgn env sgn]
adamc@88 779 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 780 space,
adamc@88 781 p_con env c1,
adamc@88 782 space,
adamc@88 783 string "~",
adamc@88 784 space,
adamc@88 785 p_con env c2]
adamc@109 786 | DExport (_, sgn, str) => box [string "export",
adamc@110 787 space,
adamc@109 788 p_str env str,
adamc@109 789 space,
adamc@109 790 string ":",
adamc@109 791 space,
adamc@109 792 p_sgn env sgn]
adamc@707 793 | DTable (_, x, n, c, pe, _, ce, _) => box [string "table",
adamc@707 794 space,
adamc@707 795 p_named x n,
adamc@707 796 space,
adamc@707 797 string ":",
adamc@707 798 space,
adamc@707 799 p_con env c,
adamc@707 800 space,
adamc@707 801 string "keys",
adamc@707 802 space,
adamc@707 803 p_exp env pe,
adamc@707 804 space,
adamc@707 805 string "constraints",
adamc@707 806 space,
adamc@707 807 p_exp env ce]
adamc@338 808 | DSequence (_, x, n) => box [string "sequence",
adamc@338 809 space,
adamc@338 810 p_named x n]
adamc@754 811 | DView (_, x, n, e, _) => box [string "view",
adamc@754 812 space,
adamc@754 813 p_named x n,
adamc@754 814 space,
adamc@754 815 string "as",
adamc@754 816 space,
adamc@754 817 p_exp env e]
adamc@563 818 | DClass (x, n, k, c) => box [string "class",
adamc@563 819 space,
adamc@563 820 p_named x n,
adamc@563 821 space,
adamc@563 822 string "::",
adamc@563 823 space,
adamc@623 824 p_kind env k,
adamc@563 825 space,
adamc@563 826 string "=",
adamc@563 827 space,
adamc@563 828 p_con env c]
adamc@271 829 | DDatabase s => box [string "database",
adamc@271 830 space,
adamc@271 831 string s]
adamc@459 832 | DCookie (_, x, n, c) => box [string "cookie",
adamc@459 833 space,
adamc@459 834 p_named x n,
adamc@459 835 space,
adamc@459 836 string ":",
adamc@459 837 space,
adamc@459 838 p_con env c]
adamc@720 839 | DStyle (_, x, n) => box [string "style",
adamc@720 840 space,
adamc@720 841 p_named x n]
adamc@1075 842 | DTask (e1, e2) => box [string "task",
adamc@1073 843 space,
adamc@1075 844 p_exp env e1,
adamc@1075 845 space,
adamc@1075 846 string "=",
adamc@1075 847 space,
adamc@1075 848 p_exp env e2]
adamc@1199 849 | DPolicy e1 => box [string "policy",
adamc@1199 850 space,
adamc@1199 851 p_exp env e1]
adam@1294 852 | DOnError _ => string "ONERROR"
adamc@31 853
adamc@31 854 and p_str env (str, _) =
adamc@31 855 case str of
adamc@31 856 StrConst ds => box [string "struct",
adamc@31 857 newline,
adamc@32 858 p_file env ds,
adamc@31 859 newline,
adamc@31 860 string "end"]
adamc@88 861 | StrVar n => ((string (#1 (E.lookupStrNamed env n)))
adamc@88 862 handle E.UnboundNamed _ => string ("UNBOUND_STR_" ^ Int.toString n))
adamc@34 863 | StrProj (str, s) => box [p_str env str,
adamc@34 864 string ".",
adamc@34 865 string s]
adamc@41 866 | StrFun (x, n, sgn, sgn', str) =>
adamc@41 867 let
adamc@41 868 val env' = E.pushStrNamedAs env x n sgn
adamc@41 869 in
adamc@41 870 box [string "functor",
adamc@41 871 space,
adamc@41 872 string "(",
adamc@41 873 string x,
adamc@41 874 space,
adamc@41 875 string ":",
adamc@41 876 space,
adamc@41 877 p_sgn env sgn,
adamc@41 878 string ")",
adamc@41 879 space,
adamc@41 880 string ":",
adamc@41 881 space,
adamc@41 882 p_sgn env' sgn',
adamc@41 883 space,
adamc@41 884 string "=>",
adamc@41 885 space,
adamc@41 886 p_str env' str]
adamc@41 887 end
adamc@44 888 | StrApp (str1, str2) => box [p_str env str1,
adamc@44 889 string "(",
adamc@44 890 p_str env str2,
adamc@44 891 string ")"]
adamc@31 892 | StrError => string "<ERROR>"
adamc@3 893
adamc@32 894 and p_file env file =
adamc@3 895 let
adamc@31 896 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 897 (p_decl env d,
adamc@31 898 E.declBinds env d))
adamc@31 899 env file
adamc@3 900 in
adamc@3 901 p_list_sep newline (fn x => x) pds
adamc@3 902 end
adamc@3 903
adamc@3 904 end