annotate src/elab_print.sml @ 343:ca98644af384

Fix inexhaustive match
author Adam Chlipala <adamc@hcoop.net>
date Sun, 14 Sep 2008 19:22:04 -0400
parents 075b36dbb1a4
children b85e6ba56618
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@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@207 52 | KTuple ks => box [string "(",
adamc@207 53 p_list_sep (box [space, string "*", space]) p_kind ks,
adamc@207 54 string ")"]
adamc@3 55
adamc@3 56 | KError => string "<ERROR>"
adamc@76 57 | KUnif (_, _, ref (SOME k)) => p_kind' par k
adamc@76 58 | KUnif (_, s, _) => string ("<UNIF:" ^ s ^ ">")
adamc@3 59
adamc@3 60 and p_kind k = p_kind' false k
adamc@3 61
adamc@3 62 fun p_explicitness e =
adamc@3 63 case e of
adamc@3 64 Explicit => string "::"
adamc@3 65 | Implicit => string ":::"
adamc@3 66
adamc@3 67 fun p_con' par env (c, _) =
adamc@3 68 case c of
adamc@3 69 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
adamc@3 70 space,
adamc@3 71 string "->",
adamc@3 72 space,
adamc@3 73 p_con env t2])
adamc@3 74 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@3 75 space,
adamc@3 76 p_explicitness e,
adamc@3 77 space,
adamc@3 78 p_kind k,
adamc@3 79 space,
adamc@3 80 string "->",
adamc@3 81 space,
adamc@3 82 p_con (E.pushCRel env x k) c])
adamc@334 83 | TDisjoint (_, c1, c2, c3) => parenIf par (box [p_con env c1,
adamc@334 84 space,
adamc@334 85 string "~",
adamc@334 86 space,
adamc@334 87 p_con env c2,
adamc@334 88 space,
adamc@334 89 string "->",
adamc@334 90 space,
adamc@334 91 p_con env c3])
adamc@3 92 | TRecord (CRecord (_, xcs), _) => box [string "{",
adamc@3 93 p_list (fn (x, c) =>
adamc@20 94 box [p_name env x,
adamc@3 95 space,
adamc@3 96 string ":",
adamc@3 97 space,
adamc@3 98 p_con env c]) xcs,
adamc@3 99 string "}"]
adamc@3 100 | TRecord c => box [string "$",
adamc@3 101 p_con' true env c]
adamc@3 102
adamc@11 103 | CRel n =>
adamc@71 104 ((if !debug then
adamc@71 105 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@71 106 else
adamc@71 107 string (#1 (E.lookupCRel env n)))
adamc@71 108 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 109 | CNamed n =>
adamc@34 110 ((if !debug then
adamc@34 111 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@34 112 else
adamc@34 113 string (#1 (E.lookupCNamed env n)))
adamc@34 114 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 115 | CModProj (m1, ms, x) =>
adamc@34 116 let
adamc@88 117 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 118 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@88 119
adamc@34 120 val m1s = if !debug then
adamc@34 121 m1x ^ "__" ^ Int.toString m1
adamc@34 122 else
adamc@34 123 m1x
adamc@34 124 in
adamc@34 125 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@88 126 end
adamc@3 127
adamc@3 128 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@3 129 space,
adamc@3 130 p_con' true env c2])
adamc@147 131 | CAbs (x, k, c) => parenIf true (box [string "fn",
adamc@147 132 space,
adamc@147 133 string x,
adamc@147 134 space,
adamc@147 135 string "::",
adamc@147 136 space,
adamc@147 137 p_kind k,
adamc@147 138 space,
adamc@147 139 string "=>",
adamc@147 140 space,
adamc@147 141 p_con (E.pushCRel env x k) c])
adamc@84 142 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con env c1,
adamc@84 143 space,
adamc@84 144 string "~",
adamc@84 145 space,
adamc@84 146 p_con env c2,
adamc@84 147 space,
adamc@84 148 string "=>",
adamc@84 149 space,
adamc@84 150 p_con env c3])
adamc@3 151
adamc@3 152 | CName s => box [string "#", string s]
adamc@3 153
adamc@12 154 | CRecord (k, xcs) =>
adamc@12 155 if !debug then
adamc@12 156 parenIf par (box [string "[",
adamc@12 157 p_list (fn (x, c) =>
adamc@12 158 box [p_con env x,
adamc@12 159 space,
adamc@12 160 string "=",
adamc@12 161 space,
adamc@12 162 p_con env c]) xcs,
adamc@12 163 string "]::",
adamc@12 164 p_kind k])
adamc@12 165 else
adamc@12 166 parenIf par (box [string "[",
adamc@12 167 p_list (fn (x, c) =>
adamc@12 168 box [p_con env x,
adamc@12 169 space,
adamc@12 170 string "=",
adamc@12 171 space,
adamc@12 172 p_con env c]) xcs,
adamc@12 173 string "]"])
adamc@3 174 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@3 175 space,
adamc@3 176 string "++",
adamc@3 177 space,
adamc@3 178 p_con env c2])
adamc@67 179 | CFold _ => string "fold"
adamc@3 180
adamc@82 181 | CUnit => string "()"
adamc@82 182
adamc@207 183 | CTuple cs => box [string "(",
adamc@207 184 p_list (p_con env) cs,
adamc@207 185 string ")"]
adamc@207 186 | CProj (c, n) => box [p_con env c,
adamc@207 187 string ".",
adamc@207 188 string (Int.toString n)]
adamc@207 189
adamc@3 190 | CError => string "<ERROR>"
adamc@76 191 | CUnif (_, _, _, ref (SOME c)) => p_con' par env c
adamc@76 192 | CUnif (_, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
adamc@76 193 p_kind k,
adamc@76 194 string ">"]
adamc@3 195
adamc@3 196 and p_con env = p_con' false env
adamc@3 197
adamc@20 198 and p_name env (all as (c, _)) =
adamc@20 199 case c of
adamc@20 200 CName s => string s
adamc@20 201 | _ => p_con env all
adamc@20 202
adamc@171 203 fun p_patCon env pc =
adamc@171 204 case pc of
adamc@171 205 PConVar n =>
adamc@171 206 ((if !debug then
adamc@171 207 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@171 208 else
adamc@171 209 string (#1 (E.lookupENamed env n)))
adamc@171 210 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@171 211 | PConProj (m1, ms, x) =>
adamc@171 212 let
adamc@171 213 val m1x = #1 (E.lookupStrNamed env m1)
adamc@171 214 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@171 215
adamc@171 216 val m1s = if !debug then
adamc@171 217 m1x ^ "__" ^ Int.toString m1
adamc@171 218 else
adamc@171 219 m1x
adamc@171 220 in
adamc@171 221 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@171 222 end
adamc@171 223
adamc@171 224 fun p_pat' par env (p, _) =
adamc@171 225 case p of
adamc@171 226 PWild => string "_"
adamc@182 227 | PVar (s, _) => string s
adamc@173 228 | PPrim p => Prim.p_t p
adamc@191 229 | PCon (_, pc, _, NONE) => p_patCon env pc
adamc@191 230 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
adamc@188 231 space,
adamc@188 232 p_pat' true env p])
adamc@176 233 | PRecord xps =>
adamc@176 234 box [string "{",
adamc@182 235 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
adamc@176 236 box [string x,
adamc@176 237 space,
adamc@176 238 string "=",
adamc@176 239 space,
adamc@176 240 p_pat env p]) xps,
adamc@176 241 string "}"]
adamc@171 242
adamc@175 243 and p_pat x = p_pat' false x
adamc@171 244
adamc@9 245 fun p_exp' par env (e, _) =
adamc@9 246 case e of
adamc@14 247 EPrim p => Prim.p_t p
adamc@14 248 | ERel n =>
adamc@88 249 ((if !debug then
adamc@88 250 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@88 251 else
adamc@88 252 string (#1 (E.lookupERel env n)))
adamc@88 253 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 254 | ENamed n =>
adamc@88 255 ((if !debug then
adamc@88 256 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@88 257 else
adamc@88 258 string (#1 (E.lookupENamed env n)))
adamc@88 259 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 260 | EModProj (m1, ms, x) =>
adamc@34 261 let
adamc@88 262 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 263 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@88 264
adamc@34 265 val m1s = if !debug then
adamc@34 266 m1x ^ "__" ^ Int.toString m1
adamc@34 267 else
adamc@34 268 m1x
adamc@34 269 in
adamc@34 270 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@34 271 end
adamc@34 272
adamc@9 273 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@9 274 space,
adamc@9 275 p_exp' true env e2])
adamc@26 276 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@26 277 space,
adamc@26 278 string x,
adamc@26 279 space,
adamc@26 280 string ":",
adamc@26 281 space,
adamc@26 282 p_con env t,
adamc@26 283 space,
adamc@26 284 string "=>",
adamc@26 285 space,
adamc@26 286 p_exp (E.pushERel env x t) e])
adamc@9 287 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@9 288 space,
adamc@9 289 string "[",
adamc@9 290 p_con env c,
adamc@9 291 string "]"])
adamc@9 292 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@9 293 space,
adamc@9 294 string x,
adamc@9 295 space,
adamc@9 296 p_explicitness exp,
adamc@9 297 space,
adamc@9 298 p_kind k,
adamc@9 299 space,
adamc@9 300 string "=>",
adamc@9 301 space,
adamc@9 302 p_exp (E.pushCRel env x k) e])
adamc@9 303
adamc@12 304 | ERecord xes => box [string "{",
adamc@29 305 p_list (fn (x, e, _) =>
adamc@21 306 box [p_name env x,
adamc@12 307 space,
adamc@12 308 string "=",
adamc@12 309 space,
adamc@12 310 p_exp env e]) xes,
adamc@12 311 string "}"]
adamc@12 312 | EField (e, c, {field, rest}) =>
adamc@12 313 if !debug then
adamc@12 314 box [p_exp' true env e,
adamc@12 315 string ".",
adamc@12 316 p_con' true env c,
adamc@12 317 space,
adamc@12 318 string "[",
adamc@12 319 p_con env field,
adamc@12 320 space,
adamc@12 321 string " in ",
adamc@12 322 space,
adamc@12 323 p_con env rest,
adamc@12 324 string "]"]
adamc@12 325 else
adamc@12 326 box [p_exp' true env e,
adamc@12 327 string ".",
adamc@12 328 p_con' true env c]
adamc@339 329 | EWith (e1, c, e2, {field, rest}) =>
adamc@339 330 parenIf par (if !debug then
adamc@339 331 box [p_exp env e1,
adamc@339 332 space,
adamc@339 333 string "with",
adamc@339 334 space,
adamc@339 335 p_con' true env c,
adamc@339 336 space,
adamc@339 337 string "=",
adamc@339 338 p_exp' true env e2,
adamc@339 339 space,
adamc@339 340 string "[",
adamc@339 341 p_con env field,
adamc@339 342 space,
adamc@339 343 string " in ",
adamc@339 344 space,
adamc@339 345 p_con env rest,
adamc@339 346 string "]"]
adamc@339 347 else
adamc@339 348 box [p_exp env e1,
adamc@339 349 space,
adamc@339 350 string "with",
adamc@339 351 space,
adamc@339 352 p_con' true env c,
adamc@339 353 space,
adamc@339 354 string "=",
adamc@339 355 space,
adamc@339 356 p_exp' true env e2])
adamc@149 357 | ECut (e, c, {field, rest}) =>
adamc@149 358 parenIf par (if !debug then
adamc@149 359 box [p_exp' true env e,
adamc@149 360 space,
adamc@149 361 string "--",
adamc@149 362 space,
adamc@149 363 p_con' true env c,
adamc@149 364 space,
adamc@149 365 string "[",
adamc@149 366 p_con env field,
adamc@149 367 space,
adamc@149 368 string " in ",
adamc@149 369 space,
adamc@149 370 p_con env rest,
adamc@149 371 string "]"]
adamc@149 372 else
adamc@149 373 box [p_exp' true env e,
adamc@149 374 space,
adamc@149 375 string "--",
adamc@149 376 space,
adamc@149 377 p_con' true env c])
adamc@72 378 | EFold _ => string "fold"
adamc@71 379
adamc@171 380 | ECase (e, pes, _) => parenIf par (box [string "case",
adamc@171 381 space,
adamc@171 382 p_exp env e,
adamc@171 383 space,
adamc@171 384 string "of",
adamc@171 385 space,
adamc@171 386 p_list_sep (box [space, string "|", space])
adamc@171 387 (fn (p, e) => box [p_pat env p,
adamc@171 388 space,
adamc@171 389 string "=>",
adamc@171 390 space,
adamc@243 391 p_exp (E.patBinds env p) e]) pes])
adamc@171 392
adamc@9 393 | EError => string "<ERROR>"
adamc@228 394 | EUnif (ref (SOME e)) => p_exp env e
adamc@228 395 | EUnif _ => string "_"
adamc@9 396
adamc@9 397 and p_exp env = p_exp' false env
adamc@9 398
adamc@31 399 fun p_named x n =
adamc@31 400 if !debug then
adamc@31 401 box [string x,
adamc@31 402 string "__",
adamc@31 403 string (Int.toString n)]
adamc@31 404 else
adamc@31 405 string x
adamc@31 406
adamc@191 407 fun p_datatype env (x, n, xs, cons) =
adamc@156 408 let
adamc@191 409 val k = (KType, ErrorMsg.dummySpan)
adamc@191 410 val env = E.pushCNamedAs env x n k NONE
adamc@191 411 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
adamc@156 412 in
adamc@156 413 box [string "datatype",
adamc@156 414 space,
adamc@156 415 string x,
adamc@191 416 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@156 417 space,
adamc@156 418 string "=",
adamc@156 419 space,
adamc@156 420 p_list_sep (box [space, string "|", space])
adamc@156 421 (fn (x, _, NONE) => string x
adamc@156 422 | (x, _, SOME t) => box [string x, space, string "of", space, p_con env t])
adamc@156 423 cons]
adamc@156 424 end
adamc@156 425
adamc@31 426 fun p_sgn_item env (sgi, _) =
adamc@31 427 case sgi of
adamc@31 428 SgiConAbs (x, n, k) => box [string "con",
adamc@31 429 space,
adamc@31 430 p_named x n,
adamc@31 431 space,
adamc@31 432 string "::",
adamc@31 433 space,
adamc@31 434 p_kind k]
adamc@31 435 | SgiCon (x, n, k, c) => box [string "con",
adamc@31 436 space,
adamc@31 437 p_named x n,
adamc@31 438 space,
adamc@31 439 string "::",
adamc@31 440 space,
adamc@31 441 p_kind k,
adamc@31 442 space,
adamc@31 443 string "=",
adamc@31 444 space,
adamc@31 445 p_con env c]
adamc@156 446 | SgiDatatype x => p_datatype env x
adamc@191 447 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@156 448 let
adamc@156 449 val m1x = #1 (E.lookupStrNamed env m1)
adamc@156 450 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@156 451 in
adamc@156 452 box [string "datatype",
adamc@156 453 space,
adamc@156 454 string x,
adamc@156 455 space,
adamc@156 456 string "=",
adamc@156 457 space,
adamc@156 458 string "datatype",
adamc@156 459 space,
adamc@156 460 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@156 461 end
adamc@31 462 | SgiVal (x, n, c) => box [string "val",
adamc@31 463 space,
adamc@31 464 p_named x n,
adamc@31 465 space,
adamc@31 466 string ":",
adamc@31 467 space,
adamc@31 468 p_con env c]
adamc@31 469 | SgiStr (x, n, sgn) => box [string "structure",
adamc@31 470 space,
adamc@31 471 p_named x n,
adamc@31 472 space,
adamc@31 473 string ":",
adamc@31 474 space,
adamc@31 475 p_sgn env sgn]
adamc@59 476 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@59 477 space,
adamc@59 478 p_named x n,
adamc@59 479 space,
adamc@59 480 string "=",
adamc@59 481 space,
adamc@59 482 p_sgn env sgn]
adamc@88 483 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 484 space,
adamc@88 485 p_con env c1,
adamc@88 486 space,
adamc@88 487 string "~",
adamc@88 488 space,
adamc@88 489 p_con env c2]
adamc@205 490 | SgiTable (_, x, n, c) => box [string "table",
adamc@205 491 space,
adamc@205 492 p_named x n,
adamc@205 493 space,
adamc@205 494 string ":",
adamc@205 495 space,
adamc@205 496 p_con env c]
adamc@338 497 | SgiSequence (_, x, n) => box [string "sequence",
adamc@338 498 space,
adamc@338 499 p_named x n]
adamc@211 500 | SgiClassAbs (x, n) => box [string "class",
adamc@211 501 space,
adamc@211 502 p_named x n]
adamc@211 503 | SgiClass (x, n, c) => box [string "class",
adamc@211 504 space,
adamc@211 505 p_named x n,
adamc@211 506 space,
adamc@211 507 string "=",
adamc@211 508 space,
adamc@211 509 p_con env c]
adamc@31 510
adamc@31 511 and p_sgn env (sgn, _) =
adamc@31 512 case sgn of
adamc@31 513 SgnConst sgis => box [string "sig",
adamc@31 514 newline,
adamc@32 515 let
adamc@32 516 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@32 517 (p_sgn_item env sgi,
adamc@32 518 E.sgiBinds env sgi))
adamc@32 519 env sgis
adamc@32 520 in
adamc@32 521 p_list_sep newline (fn x => x) psgis
adamc@32 522 end,
adamc@31 523 newline,
adamc@31 524 string "end"]
adamc@88 525 | SgnVar n => ((string (#1 (E.lookupSgnNamed env n)))
adamc@88 526 handle E.UnboundNamed _ => string ("UNBOUND_SGN_" ^ Int.toString n))
adamc@41 527 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@41 528 space,
adamc@41 529 string "(",
adamc@41 530 string x,
adamc@41 531 space,
adamc@41 532 string ":",
adamc@41 533 space,
adamc@41 534 p_sgn env sgn,
adamc@41 535 string ")",
adamc@41 536 space,
adamc@41 537 string ":",
adamc@41 538 space,
adamc@41 539 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
adamc@42 540 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@42 541 space,
adamc@42 542 string "where",
adamc@42 543 space,
adamc@42 544 string "con",
adamc@42 545 space,
adamc@42 546 string x,
adamc@42 547 space,
adamc@42 548 string "=",
adamc@42 549 space,
adamc@42 550 p_con env c]
adamc@59 551 | SgnProj (m1, ms, x) =>
adamc@59 552 let
adamc@88 553 val m1x = #1 (E.lookupStrNamed env m1)
adamc@88 554 handle E.UnboundNamed _ => "UNBOUND_SGN_" ^ Int.toString m1
adamc@88 555
adamc@59 556 val m1s = if !debug then
adamc@59 557 m1x ^ "__" ^ Int.toString m1
adamc@59 558 else
adamc@59 559 m1x
adamc@88 560 in
adamc@59 561 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@59 562 end
adamc@31 563 | SgnError => string "<ERROR>"
adamc@31 564
adamc@123 565 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@123 566 space,
adamc@123 567 string ":",
adamc@123 568 space,
adamc@123 569 p_con env t,
adamc@123 570 space,
adamc@123 571 string "=",
adamc@123 572 space,
adamc@123 573 p_exp env e]
adamc@123 574
adamc@123 575 fun p_decl env (dAll as (d, _) : decl) =
adamc@3 576 case d of
adamc@31 577 DCon (x, n, k, c) => box [string "con",
adamc@31 578 space,
adamc@31 579 p_named x n,
adamc@31 580 space,
adamc@31 581 string "::",
adamc@31 582 space,
adamc@31 583 p_kind k,
adamc@31 584 space,
adamc@31 585 string "=",
adamc@31 586 space,
adamc@31 587 p_con env c]
adamc@156 588 | DDatatype x => p_datatype env x
adamc@191 589 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@156 590 let
adamc@156 591 val m1x = #1 (E.lookupStrNamed env m1)
adamc@156 592 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@156 593 in
adamc@156 594 box [string "datatype",
adamc@156 595 space,
adamc@156 596 string x,
adamc@156 597 space,
adamc@156 598 string "=",
adamc@156 599 space,
adamc@156 600 string "datatype",
adamc@156 601 space,
adamc@156 602 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@156 603 end
adamc@123 604 | DVal vi => box [string "val",
adamc@123 605 space,
adamc@123 606 p_vali env vi]
adamc@123 607 | DValRec vis =>
adamc@123 608 let
adamc@123 609 val env = E.declBinds env dAll
adamc@123 610 in
adamc@123 611 box [string "val",
adamc@123 612 space,
adamc@123 613 string "rec",
adamc@123 614 space,
adamc@123 615 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@123 616 end
adamc@31 617
adamc@31 618 | DSgn (x, n, sgn) => box [string "signature",
adamc@31 619 space,
adamc@31 620 p_named x n,
adamc@31 621 space,
adamc@31 622 string "=",
adamc@31 623 space,
adamc@31 624 p_sgn env sgn]
adamc@31 625 | DStr (x, n, sgn, str) => box [string "structure",
adamc@31 626 space,
adamc@31 627 p_named x n,
adamc@31 628 space,
adamc@31 629 string ":",
adamc@31 630 space,
adamc@31 631 p_sgn env sgn,
adamc@31 632 space,
adamc@31 633 string "=",
adamc@31 634 space,
adamc@31 635 p_str env str]
adamc@48 636 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 637 space,
adamc@48 638 string "structure",
adamc@48 639 space,
adamc@48 640 p_named x n,
adamc@48 641 space,
adamc@48 642 string ":",
adamc@48 643 space,
adamc@48 644 p_sgn env sgn]
adamc@88 645 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 646 space,
adamc@88 647 p_con env c1,
adamc@88 648 space,
adamc@88 649 string "~",
adamc@88 650 space,
adamc@88 651 p_con env c2]
adamc@109 652 | DExport (_, sgn, str) => box [string "export",
adamc@110 653 space,
adamc@109 654 p_str env str,
adamc@109 655 space,
adamc@109 656 string ":",
adamc@109 657 space,
adamc@109 658 p_sgn env sgn]
adamc@205 659 | DTable (_, x, n, c) => box [string "table",
adamc@205 660 space,
adamc@205 661 p_named x n,
adamc@205 662 space,
adamc@205 663 string ":",
adamc@205 664 space,
adamc@205 665 p_con env c]
adamc@338 666 | DSequence (_, x, n) => box [string "sequence",
adamc@338 667 space,
adamc@338 668 p_named x n]
adamc@271 669 | DClass (x, n, c) => box [string "class",
adamc@271 670 space,
adamc@271 671 p_named x n,
adamc@271 672 space,
adamc@271 673 string "=",
adamc@271 674 space,
adamc@271 675 p_con env c]
adamc@271 676 | DDatabase s => box [string "database",
adamc@271 677 space,
adamc@271 678 string s]
adamc@31 679
adamc@31 680 and p_str env (str, _) =
adamc@31 681 case str of
adamc@31 682 StrConst ds => box [string "struct",
adamc@31 683 newline,
adamc@32 684 p_file env ds,
adamc@31 685 newline,
adamc@31 686 string "end"]
adamc@88 687 | StrVar n => ((string (#1 (E.lookupStrNamed env n)))
adamc@88 688 handle E.UnboundNamed _ => string ("UNBOUND_STR_" ^ Int.toString n))
adamc@34 689 | StrProj (str, s) => box [p_str env str,
adamc@34 690 string ".",
adamc@34 691 string s]
adamc@41 692 | StrFun (x, n, sgn, sgn', str) =>
adamc@41 693 let
adamc@41 694 val env' = E.pushStrNamedAs env x n sgn
adamc@41 695 in
adamc@41 696 box [string "functor",
adamc@41 697 space,
adamc@41 698 string "(",
adamc@41 699 string x,
adamc@41 700 space,
adamc@41 701 string ":",
adamc@41 702 space,
adamc@41 703 p_sgn env sgn,
adamc@41 704 string ")",
adamc@41 705 space,
adamc@41 706 string ":",
adamc@41 707 space,
adamc@41 708 p_sgn env' sgn',
adamc@41 709 space,
adamc@41 710 string "=>",
adamc@41 711 space,
adamc@41 712 p_str env' str]
adamc@41 713 end
adamc@44 714 | StrApp (str1, str2) => box [p_str env str1,
adamc@44 715 string "(",
adamc@44 716 p_str env str2,
adamc@44 717 string ")"]
adamc@31 718 | StrError => string "<ERROR>"
adamc@3 719
adamc@32 720 and p_file env file =
adamc@3 721 let
adamc@31 722 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 723 (p_decl env d,
adamc@31 724 E.declBinds env d))
adamc@31 725 env file
adamc@3 726 in
adamc@3 727 p_list_sep newline (fn x => x) pds
adamc@3 728 end
adamc@3 729
adamc@3 730 end