annotate src/elab_print.sml @ 1302:d008c4c43a0a

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