annotate src/elab_print.sml @ 84:e86370850c30

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