annotate src/elab_print.sml @ 1272:56bd4a4f6e66

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