annotate src/expl_print.sml @ 168:25b169416ea8

Storing datatype constructors in type references past monoize
author Adam Chlipala <adamc@hcoop.net>
date Tue, 29 Jul 2008 15:43:17 -0400
parents 80192edca30d
children 33d4a8eea484
rev   line source
adamc@38 1 (* Copyright (c) 2008, Adam Chlipala
adamc@38 2 * All rights reserved.
adamc@38 3 *
adamc@38 4 * Redistribution and use in source and binary forms, with or without
adamc@38 5 * modification, are permitted provided that the following conditions are met:
adamc@38 6 *
adamc@38 7 * - Redistributions of source code must retain the above copyright notice,
adamc@38 8 * this list of conditions and the following disclaimer.
adamc@38 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@38 10 * this list of conditions and the following disclaimer in the documentation
adamc@38 11 * and/or other materials provided with the distribution.
adamc@38 12 * - The names of contributors may not be used to endorse or promote products
adamc@38 13 * derived from this software without specific prior written permission.
adamc@38 14 *
adamc@38 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@38 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@38 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@38 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@38 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@38 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@38 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@38 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@38 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@38 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@38 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@38 26 *)
adamc@38 27
adamc@38 28 (* Pretty-printing elaborated Laconic/Web *)
adamc@38 29
adamc@38 30 structure ExplPrint :> EXPL_PRINT = struct
adamc@38 31
adamc@38 32 open Print.PD
adamc@38 33 open Print
adamc@38 34
adamc@38 35 open Expl
adamc@38 36
adamc@38 37 structure E = ExplEnv
adamc@38 38
adamc@38 39 val debug = ref false
adamc@38 40
adamc@38 41 fun p_kind' par (k, _) =
adamc@38 42 case k of
adamc@38 43 KType => string "Type"
adamc@38 44 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@38 45 space,
adamc@38 46 string "->",
adamc@38 47 space,
adamc@38 48 p_kind k2])
adamc@38 49 | KName => string "Name"
adamc@38 50 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@87 51 | KUnit => string "Unit"
adamc@38 52
adamc@38 53 and p_kind k = p_kind' false k
adamc@38 54
adamc@38 55 fun p_con' par env (c, _) =
adamc@38 56 case c of
adamc@38 57 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
adamc@38 58 space,
adamc@38 59 string "->",
adamc@38 60 space,
adamc@38 61 p_con env t2])
adamc@38 62 | TCFun (x, k, c) => parenIf par (box [string x,
adamc@38 63 space,
adamc@38 64 string "::",
adamc@38 65 space,
adamc@38 66 p_kind k,
adamc@38 67 space,
adamc@38 68 string "->",
adamc@38 69 space,
adamc@38 70 p_con (E.pushCRel env x k) c])
adamc@38 71 | TRecord (CRecord (_, xcs), _) => box [string "{",
adamc@38 72 p_list (fn (x, c) =>
adamc@38 73 box [p_name env x,
adamc@38 74 space,
adamc@38 75 string ":",
adamc@38 76 space,
adamc@38 77 p_con env c]) xcs,
adamc@38 78 string "}"]
adamc@38 79 | TRecord c => box [string "$",
adamc@38 80 p_con' true env c]
adamc@38 81
adamc@38 82 | CRel n =>
adamc@38 83 if !debug then
adamc@38 84 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@38 85 else
adamc@38 86 string (#1 (E.lookupCRel env n))
adamc@38 87 | CNamed n =>
adamc@38 88 ((if !debug then
adamc@38 89 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@38 90 else
adamc@38 91 string (#1 (E.lookupCNamed env n)))
adamc@38 92 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@38 93 | CModProj (m1, ms, x) =>
adamc@38 94 let
adamc@109 95 val m1x = #1 (E.lookupStrNamed env m1)
adamc@109 96 handle E.UnboundNamed _ => "UNBOUND"
adamc@38 97
adamc@38 98 val m1s = if !debug then
adamc@38 99 m1x ^ "__" ^ Int.toString m1
adamc@38 100 else
adamc@38 101 m1x
adamc@38 102 in
adamc@146 103 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 104 end
adamc@38 105
adamc@38 106 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@38 107 space,
adamc@38 108 p_con' true env c2])
adamc@38 109 | CAbs (x, k, c) => parenIf par (box [string "fn",
adamc@38 110 space,
adamc@38 111 string x,
adamc@38 112 space,
adamc@38 113 string "::",
adamc@38 114 space,
adamc@38 115 p_kind k,
adamc@38 116 space,
adamc@38 117 string "=>",
adamc@38 118 space,
adamc@38 119 p_con (E.pushCRel env x k) c])
adamc@38 120
adamc@38 121 | CName s => box [string "#", string s]
adamc@38 122
adamc@38 123 | CRecord (k, xcs) =>
adamc@38 124 if !debug then
adamc@38 125 parenIf par (box [string "[",
adamc@38 126 p_list (fn (x, c) =>
adamc@38 127 box [p_con env x,
adamc@38 128 space,
adamc@38 129 string "=",
adamc@38 130 space,
adamc@38 131 p_con env c]) xcs,
adamc@38 132 string "]::",
adamc@38 133 p_kind k])
adamc@38 134 else
adamc@38 135 parenIf par (box [string "[",
adamc@38 136 p_list (fn (x, c) =>
adamc@38 137 box [p_con env x,
adamc@38 138 space,
adamc@38 139 string "=",
adamc@38 140 space,
adamc@38 141 p_con env c]) xcs,
adamc@38 142 string "]"])
adamc@38 143 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@38 144 space,
adamc@38 145 string "++",
adamc@38 146 space,
adamc@38 147 p_con env c2])
adamc@68 148 | CFold _ => string "fold"
adamc@87 149 | CUnit => string "()"
adamc@38 150
adamc@38 151 and p_con env = p_con' false env
adamc@38 152
adamc@38 153 and p_name env (all as (c, _)) =
adamc@38 154 case c of
adamc@38 155 CName s => string s
adamc@38 156 | _ => p_con env all
adamc@38 157
adamc@146 158 fun p_exp' par env (e, loc) =
adamc@38 159 case e of
adamc@38 160 EPrim p => Prim.p_t p
adamc@38 161 | ERel n =>
adamc@38 162 if !debug then
adamc@38 163 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@38 164 else
adamc@38 165 string (#1 (E.lookupERel env n))
adamc@38 166 | ENamed n =>
adamc@38 167 if !debug then
adamc@38 168 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@38 169 else
adamc@38 170 string (#1 (E.lookupENamed env n))
adamc@38 171 | EModProj (m1, ms, x) =>
adamc@38 172 let
adamc@38 173 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 174 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@38 175
adamc@38 176 val m1s = if !debug then
adamc@38 177 m1x ^ "__" ^ Int.toString m1
adamc@38 178 else
adamc@38 179 m1x
adamc@38 180 in
adamc@146 181 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 182 end
adamc@38 183
adamc@38 184 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@38 185 space,
adamc@38 186 p_exp' true env e2])
adamc@38 187 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@38 188 space,
adamc@38 189 string x,
adamc@38 190 space,
adamc@38 191 string ":",
adamc@38 192 space,
adamc@38 193 p_con env t,
adamc@38 194 space,
adamc@38 195 string "=>",
adamc@38 196 space,
adamc@38 197 p_exp (E.pushERel env x t) e])
adamc@38 198 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@38 199 space,
adamc@38 200 string "[",
adamc@38 201 p_con env c,
adamc@38 202 string "]"])
adamc@38 203 | ECAbs (x, k, e) => parenIf par (box [string "fn",
adamc@38 204 space,
adamc@38 205 string x,
adamc@38 206 space,
adamc@38 207 string "::",
adamc@38 208 space,
adamc@38 209 p_kind k,
adamc@38 210 space,
adamc@38 211 string "=>",
adamc@38 212 space,
adamc@38 213 p_exp (E.pushCRel env x k) e])
adamc@38 214
adamc@38 215 | ERecord xes => box [string "{",
adamc@38 216 p_list (fn (x, e, _) =>
adamc@38 217 box [p_name env x,
adamc@38 218 space,
adamc@38 219 string "=",
adamc@38 220 space,
adamc@38 221 p_exp env e]) xes,
adamc@38 222 string "}"]
adamc@38 223 | EField (e, c, {field, rest}) =>
adamc@38 224 if !debug then
adamc@38 225 box [p_exp' true env e,
adamc@38 226 string ".",
adamc@38 227 p_con' true env c,
adamc@38 228 space,
adamc@38 229 string "[",
adamc@38 230 p_con env field,
adamc@38 231 space,
adamc@38 232 string " in ",
adamc@38 233 space,
adamc@38 234 p_con env rest,
adamc@38 235 string "]"]
adamc@38 236 else
adamc@38 237 box [p_exp' true env e,
adamc@38 238 string ".",
adamc@38 239 p_con' true env c]
adamc@149 240 | ECut (e, c, {field, rest}) =>
adamc@149 241 parenIf par (if !debug then
adamc@149 242 box [p_exp' true env e,
adamc@149 243 space,
adamc@149 244 string "--",
adamc@149 245 space,
adamc@149 246 p_con' true env c,
adamc@149 247 space,
adamc@149 248 string "[",
adamc@149 249 p_con env field,
adamc@149 250 space,
adamc@149 251 string " in ",
adamc@149 252 space,
adamc@149 253 p_con env rest,
adamc@149 254 string "]"]
adamc@149 255 else
adamc@149 256 box [p_exp' true env e,
adamc@149 257 space,
adamc@149 258 string "--",
adamc@149 259 space,
adamc@149 260 p_con' true env c])
adamc@72 261 | EFold _ => string "fold"
adamc@38 262
adamc@109 263 | EWrite e => box [string "write(",
adamc@109 264 p_exp env e,
adamc@109 265 string ")"]
adamc@109 266
adamc@38 267 and p_exp env = p_exp' false env
adamc@38 268
adamc@38 269 fun p_named x n =
adamc@38 270 if !debug then
adamc@38 271 box [string x,
adamc@38 272 string "__",
adamc@38 273 string (Int.toString n)]
adamc@38 274 else
adamc@38 275 string x
adamc@38 276
adamc@162 277 fun p_datatype env (x, n, cons) =
adamc@162 278 let
adamc@162 279 val env = E.pushCNamed env x n (KType, ErrorMsg.dummySpan) NONE
adamc@162 280 in
adamc@162 281 box [string "datatype",
adamc@162 282 space,
adamc@162 283 string x,
adamc@162 284 space,
adamc@162 285 string "=",
adamc@162 286 space,
adamc@162 287 p_list_sep (box [space, string "|", space])
adamc@163 288 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 289 else string x
adamc@163 290 | (x, _, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 291 else string x, space, string "of", space, p_con env t])
adamc@162 292 cons]
adamc@162 293 end
adamc@162 294
adamc@38 295 fun p_sgn_item env (sgi, _) =
adamc@38 296 case sgi of
adamc@38 297 SgiConAbs (x, n, k) => box [string "con",
adamc@38 298 space,
adamc@38 299 p_named x n,
adamc@38 300 space,
adamc@38 301 string "::",
adamc@38 302 space,
adamc@38 303 p_kind k]
adamc@38 304 | SgiCon (x, n, k, c) => box [string "con",
adamc@38 305 space,
adamc@38 306 p_named x n,
adamc@38 307 space,
adamc@38 308 string "::",
adamc@38 309 space,
adamc@38 310 p_kind k,
adamc@38 311 space,
adamc@38 312 string "=",
adamc@38 313 space,
adamc@38 314 p_con env c]
adamc@162 315 | SgiDatatype x => p_datatype env x
adamc@162 316 | SgiDatatypeImp (x, _, m1, ms, x', _) =>
adamc@162 317 let
adamc@162 318 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 319 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 320 in
adamc@162 321 box [string "datatype",
adamc@162 322 space,
adamc@162 323 string x,
adamc@162 324 space,
adamc@162 325 string "=",
adamc@162 326 space,
adamc@162 327 string "datatype",
adamc@162 328 space,
adamc@162 329 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 330 end
adamc@38 331 | SgiVal (x, n, c) => box [string "val",
adamc@38 332 space,
adamc@38 333 p_named x n,
adamc@38 334 space,
adamc@38 335 string ":",
adamc@38 336 space,
adamc@38 337 p_con env c]
adamc@38 338 | SgiStr (x, n, sgn) => box [string "structure",
adamc@38 339 space,
adamc@38 340 p_named x n,
adamc@38 341 space,
adamc@38 342 string ":",
adamc@38 343 space,
adamc@38 344 p_sgn env sgn]
adamc@64 345 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@64 346 space,
adamc@64 347 p_named x n,
adamc@64 348 space,
adamc@64 349 string "=",
adamc@64 350 space,
adamc@64 351 p_sgn env sgn]
adamc@38 352
adamc@146 353 and p_sgn env (sgn, loc) =
adamc@38 354 case sgn of
adamc@38 355 SgnConst sgis => box [string "sig",
adamc@38 356 newline,
adamc@38 357 let
adamc@38 358 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@38 359 (p_sgn_item env sgi,
adamc@38 360 E.sgiBinds env sgi))
adamc@38 361 env sgis
adamc@38 362 in
adamc@38 363 p_list_sep newline (fn x => x) psgis
adamc@38 364 end,
adamc@38 365 newline,
adamc@38 366 string "end"]
adamc@146 367 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
adamc@146 368 handle E.UnboundNamed _ => "UNBOUND")
adamc@45 369 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@45 370 space,
adamc@45 371 string "(",
adamc@45 372 string x,
adamc@45 373 space,
adamc@45 374 string ":",
adamc@45 375 space,
adamc@45 376 p_sgn env sgn,
adamc@45 377 string ")",
adamc@45 378 space,
adamc@45 379 string ":",
adamc@45 380 space,
adamc@45 381 p_sgn (E.pushStrNamed env x n sgn) sgn']
adamc@45 382 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@45 383 space,
adamc@45 384 string "where",
adamc@45 385 space,
adamc@45 386 string "con",
adamc@45 387 space,
adamc@45 388 string x,
adamc@45 389 space,
adamc@45 390 string "=",
adamc@45 391 space,
adamc@45 392 p_con env c]
adamc@64 393 | SgnProj (m1, ms, x) =>
adamc@64 394 let
adamc@64 395 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 396 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@64 397
adamc@64 398 val m1s = if !debug then
adamc@64 399 m1x ^ "__" ^ Int.toString m1
adamc@64 400 else
adamc@64 401 m1x
adamc@64 402 in
adamc@64 403 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@64 404 end
adamc@38 405
adamc@124 406 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@124 407 space,
adamc@124 408 string ":",
adamc@124 409 space,
adamc@124 410 p_con env t,
adamc@124 411 space,
adamc@124 412 string "=",
adamc@124 413 space,
adamc@124 414 p_exp env e]
adamc@124 415
adamc@124 416 fun p_decl env (dAll as (d, _) : decl) =
adamc@38 417 case d of
adamc@38 418 DCon (x, n, k, c) => box [string "con",
adamc@38 419 space,
adamc@38 420 p_named x n,
adamc@38 421 space,
adamc@38 422 string "::",
adamc@38 423 space,
adamc@38 424 p_kind k,
adamc@38 425 space,
adamc@38 426 string "=",
adamc@38 427 space,
adamc@38 428 p_con env c]
adamc@162 429 | DDatatype x => p_datatype env x
adamc@162 430 | DDatatypeImp (x, _, m1, ms, x', _) =>
adamc@162 431 let
adamc@162 432 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 433 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 434 in
adamc@162 435 box [string "datatype",
adamc@162 436 space,
adamc@162 437 string x,
adamc@162 438 space,
adamc@162 439 string "=",
adamc@162 440 space,
adamc@162 441 string "datatype",
adamc@162 442 space,
adamc@162 443 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 444 end
adamc@124 445 | DVal vi => box [string "val",
adamc@124 446 space,
adamc@124 447 p_vali env vi]
adamc@124 448 | DValRec vis =>
adamc@124 449 let
adamc@124 450 val env = E.declBinds env dAll
adamc@124 451 in
adamc@124 452 box [string "val",
adamc@124 453 space,
adamc@124 454 string "rec",
adamc@124 455 space,
adamc@124 456 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@124 457 end
adamc@38 458
adamc@38 459 | DSgn (x, n, sgn) => box [string "signature",
adamc@38 460 space,
adamc@38 461 p_named x n,
adamc@38 462 space,
adamc@38 463 string "=",
adamc@38 464 space,
adamc@38 465 p_sgn env sgn]
adamc@38 466 | DStr (x, n, sgn, str) => box [string "structure",
adamc@38 467 space,
adamc@38 468 p_named x n,
adamc@38 469 space,
adamc@38 470 string ":",
adamc@38 471 space,
adamc@38 472 p_sgn env sgn,
adamc@38 473 space,
adamc@38 474 string "=",
adamc@38 475 space,
adamc@38 476 p_str env str]
adamc@48 477 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 478 space,
adamc@48 479 string "structure",
adamc@48 480 space,
adamc@48 481 p_named x n,
adamc@48 482 space,
adamc@48 483 string ":",
adamc@48 484 space,
adamc@48 485 p_sgn env sgn]
adamc@109 486 | DExport (_, sgn, str) => box [string "export",
adamc@109 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@38 493
adamc@38 494 and p_str env (str, _) =
adamc@38 495 case str of
adamc@38 496 StrConst ds => box [string "struct",
adamc@38 497 newline,
adamc@38 498 p_file env ds,
adamc@38 499 newline,
adamc@38 500 string "end"]
adamc@146 501 | StrVar n =>
adamc@146 502 let
adamc@146 503 val x = #1 (E.lookupStrNamed env n)
adamc@146 504 handle E.UnboundNamed _ => "UNBOUND"
adamc@146 505
adamc@146 506 val s = if !debug then
adamc@146 507 x ^ "__" ^ Int.toString n
adamc@146 508 else
adamc@146 509 x
adamc@146 510 in
adamc@146 511 string s
adamc@146 512 end
adamc@38 513 | StrProj (str, s) => box [p_str env str,
adamc@38 514 string ".",
adamc@38 515 string s]
adamc@45 516 | StrFun (x, n, sgn, sgn', str) =>
adamc@45 517 let
adamc@45 518 val env' = E.pushStrNamed env x n sgn
adamc@45 519 in
adamc@45 520 box [string "functor",
adamc@45 521 space,
adamc@45 522 string "(",
adamc@45 523 string x,
adamc@45 524 space,
adamc@45 525 string ":",
adamc@45 526 space,
adamc@45 527 p_sgn env sgn,
adamc@45 528 string ")",
adamc@45 529 space,
adamc@45 530 string ":",
adamc@45 531 space,
adamc@45 532 p_sgn env' sgn',
adamc@45 533 space,
adamc@45 534 string "=>",
adamc@45 535 space,
adamc@45 536 p_str env' str]
adamc@45 537 end
adamc@45 538 | StrApp (str1, str2) => box [p_str env str1,
adamc@45 539 string "(",
adamc@45 540 p_str env str2,
adamc@45 541 string ")"]
adamc@38 542
adamc@38 543 and p_file env file =
adamc@38 544 let
adamc@38 545 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@38 546 (p_decl env d,
adamc@38 547 E.declBinds env d))
adamc@38 548 env file
adamc@38 549 in
adamc@38 550 p_list_sep newline (fn x => x) pds
adamc@38 551 end
adamc@38 552
adamc@38 553 end