annotate src/elab_print.sml @ 149:7420fa18d657

Record cut
author Adam Chlipala <adamc@hcoop.net>
date Thu, 24 Jul 2008 10:09:21 -0400
parents eb16f2aadbe9
children 34ccd7d2bea8
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@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@9 193 fun p_exp' par env (e, _) =
adamc@9 194 case e of
adamc@14 195 EPrim p => Prim.p_t p
adamc@14 196 | ERel n =>
adamc@88 197 ((if !debug then
adamc@88 198 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@88 199 else
adamc@88 200 string (#1 (E.lookupERel env n)))
adamc@88 201 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 202 | ENamed n =>
adamc@88 203 ((if !debug then
adamc@88 204 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@88 205 else
adamc@88 206 string (#1 (E.lookupENamed env n)))
adamc@88 207 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 208 | EModProj (m1, ms, x) =>
adamc@34 209 let
adamc@88 210 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 211 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@88 212
adamc@34 213 val m1s = if !debug then
adamc@34 214 m1x ^ "__" ^ Int.toString m1
adamc@34 215 else
adamc@34 216 m1x
adamc@34 217 in
adamc@34 218 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@34 219 end
adamc@34 220
adamc@9 221 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@9 222 space,
adamc@9 223 p_exp' true env e2])
adamc@26 224 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@26 225 space,
adamc@26 226 string x,
adamc@26 227 space,
adamc@26 228 string ":",
adamc@26 229 space,
adamc@26 230 p_con env t,
adamc@26 231 space,
adamc@26 232 string "=>",
adamc@26 233 space,
adamc@26 234 p_exp (E.pushERel env x t) e])
adamc@9 235 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@9 236 space,
adamc@9 237 string "[",
adamc@9 238 p_con env c,
adamc@9 239 string "]"])
adamc@9 240 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@9 241 space,
adamc@9 242 string x,
adamc@9 243 space,
adamc@9 244 p_explicitness exp,
adamc@9 245 space,
adamc@9 246 p_kind k,
adamc@9 247 space,
adamc@9 248 string "=>",
adamc@9 249 space,
adamc@9 250 p_exp (E.pushCRel env x k) e])
adamc@9 251
adamc@12 252 | ERecord xes => box [string "{",
adamc@29 253 p_list (fn (x, e, _) =>
adamc@21 254 box [p_name env x,
adamc@12 255 space,
adamc@12 256 string "=",
adamc@12 257 space,
adamc@12 258 p_exp env e]) xes,
adamc@12 259 string "}"]
adamc@12 260 | EField (e, c, {field, rest}) =>
adamc@12 261 if !debug then
adamc@12 262 box [p_exp' true env e,
adamc@12 263 string ".",
adamc@12 264 p_con' true env c,
adamc@12 265 space,
adamc@12 266 string "[",
adamc@12 267 p_con env field,
adamc@12 268 space,
adamc@12 269 string " in ",
adamc@12 270 space,
adamc@12 271 p_con env rest,
adamc@12 272 string "]"]
adamc@12 273 else
adamc@12 274 box [p_exp' true env e,
adamc@12 275 string ".",
adamc@12 276 p_con' true env c]
adamc@149 277 | ECut (e, c, {field, rest}) =>
adamc@149 278 parenIf par (if !debug then
adamc@149 279 box [p_exp' true env e,
adamc@149 280 space,
adamc@149 281 string "--",
adamc@149 282 space,
adamc@149 283 p_con' true env c,
adamc@149 284 space,
adamc@149 285 string "[",
adamc@149 286 p_con env field,
adamc@149 287 space,
adamc@149 288 string " in ",
adamc@149 289 space,
adamc@149 290 p_con env rest,
adamc@149 291 string "]"]
adamc@149 292 else
adamc@149 293 box [p_exp' true env e,
adamc@149 294 space,
adamc@149 295 string "--",
adamc@149 296 space,
adamc@149 297 p_con' true env c])
adamc@72 298 | EFold _ => string "fold"
adamc@71 299
adamc@9 300 | EError => string "<ERROR>"
adamc@9 301
adamc@9 302 and p_exp env = p_exp' false env
adamc@9 303
adamc@31 304 fun p_named x n =
adamc@31 305 if !debug then
adamc@31 306 box [string x,
adamc@31 307 string "__",
adamc@31 308 string (Int.toString n)]
adamc@31 309 else
adamc@31 310 string x
adamc@31 311
adamc@31 312 fun p_sgn_item env (sgi, _) =
adamc@31 313 case sgi of
adamc@31 314 SgiConAbs (x, n, k) => box [string "con",
adamc@31 315 space,
adamc@31 316 p_named x n,
adamc@31 317 space,
adamc@31 318 string "::",
adamc@31 319 space,
adamc@31 320 p_kind k]
adamc@31 321 | SgiCon (x, n, k, c) => box [string "con",
adamc@31 322 space,
adamc@31 323 p_named x n,
adamc@31 324 space,
adamc@31 325 string "::",
adamc@31 326 space,
adamc@31 327 p_kind k,
adamc@31 328 space,
adamc@31 329 string "=",
adamc@31 330 space,
adamc@31 331 p_con env c]
adamc@31 332 | SgiVal (x, n, c) => box [string "val",
adamc@31 333 space,
adamc@31 334 p_named x n,
adamc@31 335 space,
adamc@31 336 string ":",
adamc@31 337 space,
adamc@31 338 p_con env c]
adamc@31 339 | SgiStr (x, n, sgn) => box [string "structure",
adamc@31 340 space,
adamc@31 341 p_named x n,
adamc@31 342 space,
adamc@31 343 string ":",
adamc@31 344 space,
adamc@31 345 p_sgn env sgn]
adamc@59 346 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@59 347 space,
adamc@59 348 p_named x n,
adamc@59 349 space,
adamc@59 350 string "=",
adamc@59 351 space,
adamc@59 352 p_sgn env sgn]
adamc@88 353 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 354 space,
adamc@88 355 p_con env c1,
adamc@88 356 space,
adamc@88 357 string "~",
adamc@88 358 space,
adamc@88 359 p_con env c2]
adamc@31 360
adamc@31 361 and p_sgn env (sgn, _) =
adamc@31 362 case sgn of
adamc@31 363 SgnConst sgis => box [string "sig",
adamc@31 364 newline,
adamc@32 365 let
adamc@32 366 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@32 367 (p_sgn_item env sgi,
adamc@32 368 E.sgiBinds env sgi))
adamc@32 369 env sgis
adamc@32 370 in
adamc@32 371 p_list_sep newline (fn x => x) psgis
adamc@32 372 end,
adamc@31 373 newline,
adamc@31 374 string "end"]
adamc@88 375 | SgnVar n => ((string (#1 (E.lookupSgnNamed env n)))
adamc@88 376 handle E.UnboundNamed _ => string ("UNBOUND_SGN_" ^ Int.toString n))
adamc@41 377 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@41 378 space,
adamc@41 379 string "(",
adamc@41 380 string x,
adamc@41 381 space,
adamc@41 382 string ":",
adamc@41 383 space,
adamc@41 384 p_sgn env sgn,
adamc@41 385 string ")",
adamc@41 386 space,
adamc@41 387 string ":",
adamc@41 388 space,
adamc@41 389 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
adamc@42 390 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@42 391 space,
adamc@42 392 string "where",
adamc@42 393 space,
adamc@42 394 string "con",
adamc@42 395 space,
adamc@42 396 string x,
adamc@42 397 space,
adamc@42 398 string "=",
adamc@42 399 space,
adamc@42 400 p_con env c]
adamc@59 401 | SgnProj (m1, ms, x) =>
adamc@59 402 let
adamc@88 403 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 404 handle E.UnboundNamed _ => "UNBOUND_SGN_" ^ Int.toString m1
adamc@88 405
adamc@59 406 val m1s = if !debug then
adamc@59 407 m1x ^ "__" ^ Int.toString m1
adamc@59 408 else
adamc@59 409 m1x
adamc@88 410 in
adamc@59 411 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@59 412 end
adamc@31 413 | SgnError => string "<ERROR>"
adamc@31 414
adamc@123 415 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@123 416 space,
adamc@123 417 string ":",
adamc@123 418 space,
adamc@123 419 p_con env t,
adamc@123 420 space,
adamc@123 421 string "=",
adamc@123 422 space,
adamc@123 423 p_exp env e]
adamc@123 424
adamc@123 425 fun p_decl env (dAll as (d, _) : decl) =
adamc@3 426 case d of
adamc@31 427 DCon (x, n, k, c) => box [string "con",
adamc@31 428 space,
adamc@31 429 p_named x n,
adamc@31 430 space,
adamc@31 431 string "::",
adamc@31 432 space,
adamc@31 433 p_kind k,
adamc@31 434 space,
adamc@31 435 string "=",
adamc@31 436 space,
adamc@31 437 p_con env c]
adamc@123 438 | DVal vi => box [string "val",
adamc@123 439 space,
adamc@123 440 p_vali env vi]
adamc@123 441 | DValRec vis =>
adamc@123 442 let
adamc@123 443 val env = E.declBinds env dAll
adamc@123 444 in
adamc@123 445 box [string "val",
adamc@123 446 space,
adamc@123 447 string "rec",
adamc@123 448 space,
adamc@123 449 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@123 450 end
adamc@31 451
adamc@31 452 | DSgn (x, n, sgn) => box [string "signature",
adamc@31 453 space,
adamc@31 454 p_named x n,
adamc@31 455 space,
adamc@31 456 string "=",
adamc@31 457 space,
adamc@31 458 p_sgn env sgn]
adamc@31 459 | DStr (x, n, sgn, str) => box [string "structure",
adamc@31 460 space,
adamc@31 461 p_named x n,
adamc@31 462 space,
adamc@31 463 string ":",
adamc@31 464 space,
adamc@31 465 p_sgn env sgn,
adamc@31 466 space,
adamc@31 467 string "=",
adamc@31 468 space,
adamc@31 469 p_str env str]
adamc@48 470 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 471 space,
adamc@48 472 string "structure",
adamc@48 473 space,
adamc@48 474 p_named x n,
adamc@48 475 space,
adamc@48 476 string ":",
adamc@48 477 space,
adamc@48 478 p_sgn env sgn]
adamc@88 479 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 480 space,
adamc@88 481 p_con env c1,
adamc@88 482 space,
adamc@88 483 string "~",
adamc@88 484 space,
adamc@88 485 p_con env c2]
adamc@109 486 | DExport (_, sgn, str) => box [string "export",
adamc@110 487 space,
adamc@109 488 p_str env str,
adamc@109 489 space,
adamc@109 490 string ":",
adamc@109 491 space,
adamc@109 492 p_sgn env sgn]
adamc@31 493
adamc@31 494 and p_str env (str, _) =
adamc@31 495 case str of
adamc@31 496 StrConst ds => box [string "struct",
adamc@31 497 newline,
adamc@32 498 p_file env ds,
adamc@31 499 newline,
adamc@31 500 string "end"]
adamc@88 501 | StrVar n => ((string (#1 (E.lookupStrNamed env n)))
adamc@88 502 handle E.UnboundNamed _ => string ("UNBOUND_STR_" ^ Int.toString n))
adamc@34 503 | StrProj (str, s) => box [p_str env str,
adamc@34 504 string ".",
adamc@34 505 string s]
adamc@41 506 | StrFun (x, n, sgn, sgn', str) =>
adamc@41 507 let
adamc@41 508 val env' = E.pushStrNamedAs env x n sgn
adamc@41 509 in
adamc@41 510 box [string "functor",
adamc@41 511 space,
adamc@41 512 string "(",
adamc@41 513 string x,
adamc@41 514 space,
adamc@41 515 string ":",
adamc@41 516 space,
adamc@41 517 p_sgn env sgn,
adamc@41 518 string ")",
adamc@41 519 space,
adamc@41 520 string ":",
adamc@41 521 space,
adamc@41 522 p_sgn env' sgn',
adamc@41 523 space,
adamc@41 524 string "=>",
adamc@41 525 space,
adamc@41 526 p_str env' str]
adamc@41 527 end
adamc@44 528 | StrApp (str1, str2) => box [p_str env str1,
adamc@44 529 string "(",
adamc@44 530 p_str env str2,
adamc@44 531 string ")"]
adamc@31 532 | StrError => string "<ERROR>"
adamc@3 533
adamc@32 534 and p_file env file =
adamc@3 535 let
adamc@31 536 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 537 (p_decl env d,
adamc@31 538 E.declBinds env d))
adamc@31 539 env file
adamc@3 540 in
adamc@3 541 p_list_sep newline (fn x => x) pds
adamc@3 542 end
adamc@3 543
adamc@3 544 end