annotate src/elab_print.sml @ 85:1f85890c9846

Disjointness assumptions in expressions
author Adam Chlipala <adamc@hcoop.net>
date Tue, 01 Jul 2008 12:25:12 -0400
parents e86370850c30
children 7bab29834cd6
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@85 80 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con env c1,
adamc@85 81 space,
adamc@85 82 string "~",
adamc@85 83 space,
adamc@85 84 p_con env c2,
adamc@85 85 space,
adamc@85 86 string "->",
adamc@85 87 space,
adamc@85 88 p_con env c3])
adamc@3 89 | TRecord (CRecord (_, xcs), _) => box [string "{",
adamc@3 90 p_list (fn (x, c) =>
adamc@20 91 box [p_name env x,
adamc@3 92 space,
adamc@3 93 string ":",
adamc@3 94 space,
adamc@3 95 p_con env c]) xcs,
adamc@3 96 string "}"]
adamc@3 97 | TRecord c => box [string "$",
adamc@3 98 p_con' true env c]
adamc@3 99
adamc@11 100 | CRel n =>
adamc@71 101 ((if !debug then
adamc@71 102 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@71 103 else
adamc@71 104 string (#1 (E.lookupCRel env n)))
adamc@71 105 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@11 106 | CNamed n =>
adamc@34 107 ((if !debug then
adamc@34 108 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@34 109 else
adamc@34 110 string (#1 (E.lookupCNamed env n)))
adamc@34 111 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@34 112 | CModProj (m1, ms, x) =>
adamc@34 113 let
adamc@34 114 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@34 115
adamc@34 116 val m1s = if !debug then
adamc@34 117 m1x ^ "__" ^ Int.toString m1
adamc@34 118 else
adamc@34 119 m1x
adamc@34 120 in
adamc@34 121 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@34 122 end
adamc@3 123
adamc@3 124 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@3 125 space,
adamc@3 126 p_con' true env c2])
adamc@8 127 | CAbs (x, k, c) => parenIf par (box [string "fn",
adamc@8 128 space,
adamc@8 129 string x,
adamc@8 130 space,
adamc@8 131 string "::",
adamc@8 132 space,
adamc@8 133 p_kind k,
adamc@8 134 space,
adamc@8 135 string "=>",
adamc@8 136 space,
adamc@8 137 p_con (E.pushCRel env x k) c])
adamc@84 138 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con env c1,
adamc@84 139 space,
adamc@84 140 string "~",
adamc@84 141 space,
adamc@84 142 p_con env c2,
adamc@84 143 space,
adamc@84 144 string "=>",
adamc@84 145 space,
adamc@84 146 p_con env c3])
adamc@3 147
adamc@3 148 | CName s => box [string "#", string s]
adamc@3 149
adamc@12 150 | CRecord (k, xcs) =>
adamc@12 151 if !debug then
adamc@12 152 parenIf par (box [string "[",
adamc@12 153 p_list (fn (x, c) =>
adamc@12 154 box [p_con env x,
adamc@12 155 space,
adamc@12 156 string "=",
adamc@12 157 space,
adamc@12 158 p_con env c]) xcs,
adamc@12 159 string "]::",
adamc@12 160 p_kind k])
adamc@12 161 else
adamc@12 162 parenIf par (box [string "[",
adamc@12 163 p_list (fn (x, c) =>
adamc@12 164 box [p_con env x,
adamc@12 165 space,
adamc@12 166 string "=",
adamc@12 167 space,
adamc@12 168 p_con env c]) xcs,
adamc@12 169 string "]"])
adamc@3 170 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@3 171 space,
adamc@3 172 string "++",
adamc@3 173 space,
adamc@3 174 p_con env c2])
adamc@67 175 | CFold _ => string "fold"
adamc@3 176
adamc@82 177 | CUnit => string "()"
adamc@82 178
adamc@3 179 | CError => string "<ERROR>"
adamc@76 180 | CUnif (_, _, _, ref (SOME c)) => p_con' par env c
adamc@76 181 | CUnif (_, k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
adamc@76 182 p_kind k,
adamc@76 183 string ">"]
adamc@3 184
adamc@3 185 and p_con env = p_con' false env
adamc@3 186
adamc@20 187 and p_name env (all as (c, _)) =
adamc@20 188 case c of
adamc@20 189 CName s => string s
adamc@20 190 | _ => p_con env all
adamc@20 191
adamc@9 192 fun p_exp' par env (e, _) =
adamc@9 193 case e of
adamc@14 194 EPrim p => Prim.p_t p
adamc@14 195 | ERel n =>
adamc@11 196 if !debug then
adamc@11 197 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@11 198 else
adamc@11 199 string (#1 (E.lookupERel env n))
adamc@11 200 | ENamed n =>
adamc@11 201 if !debug then
adamc@11 202 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@11 203 else
adamc@11 204 string (#1 (E.lookupENamed env n))
adamc@34 205 | EModProj (m1, ms, x) =>
adamc@34 206 let
adamc@34 207 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@34 208
adamc@34 209 val m1s = if !debug then
adamc@34 210 m1x ^ "__" ^ Int.toString m1
adamc@34 211 else
adamc@34 212 m1x
adamc@34 213 in
adamc@34 214 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@34 215 end
adamc@34 216
adamc@9 217 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@9 218 space,
adamc@9 219 p_exp' true env e2])
adamc@26 220 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@26 221 space,
adamc@26 222 string x,
adamc@26 223 space,
adamc@26 224 string ":",
adamc@26 225 space,
adamc@26 226 p_con env t,
adamc@26 227 space,
adamc@26 228 string "=>",
adamc@26 229 space,
adamc@26 230 p_exp (E.pushERel env x t) e])
adamc@9 231 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@9 232 space,
adamc@9 233 string "[",
adamc@9 234 p_con env c,
adamc@9 235 string "]"])
adamc@9 236 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@9 237 space,
adamc@9 238 string x,
adamc@9 239 space,
adamc@9 240 p_explicitness exp,
adamc@9 241 space,
adamc@9 242 p_kind k,
adamc@9 243 space,
adamc@9 244 string "=>",
adamc@9 245 space,
adamc@9 246 p_exp (E.pushCRel env x k) e])
adamc@9 247
adamc@12 248 | ERecord xes => box [string "{",
adamc@29 249 p_list (fn (x, e, _) =>
adamc@21 250 box [p_name env x,
adamc@12 251 space,
adamc@12 252 string "=",
adamc@12 253 space,
adamc@12 254 p_exp env e]) xes,
adamc@12 255 string "}"]
adamc@12 256 | EField (e, c, {field, rest}) =>
adamc@12 257 if !debug then
adamc@12 258 box [p_exp' true env e,
adamc@12 259 string ".",
adamc@12 260 p_con' true env c,
adamc@12 261 space,
adamc@12 262 string "[",
adamc@12 263 p_con env field,
adamc@12 264 space,
adamc@12 265 string " in ",
adamc@12 266 space,
adamc@12 267 p_con env rest,
adamc@12 268 string "]"]
adamc@12 269 else
adamc@12 270 box [p_exp' true env e,
adamc@12 271 string ".",
adamc@12 272 p_con' true env c]
adamc@72 273 | EFold _ => string "fold"
adamc@71 274
adamc@9 275 | EError => string "<ERROR>"
adamc@9 276
adamc@9 277 and p_exp env = p_exp' false env
adamc@9 278
adamc@31 279 fun p_named x n =
adamc@31 280 if !debug then
adamc@31 281 box [string x,
adamc@31 282 string "__",
adamc@31 283 string (Int.toString n)]
adamc@31 284 else
adamc@31 285 string x
adamc@31 286
adamc@31 287 fun p_sgn_item env (sgi, _) =
adamc@31 288 case sgi of
adamc@31 289 SgiConAbs (x, n, k) => box [string "con",
adamc@31 290 space,
adamc@31 291 p_named x n,
adamc@31 292 space,
adamc@31 293 string "::",
adamc@31 294 space,
adamc@31 295 p_kind k]
adamc@31 296 | SgiCon (x, n, k, c) => box [string "con",
adamc@31 297 space,
adamc@31 298 p_named x n,
adamc@31 299 space,
adamc@31 300 string "::",
adamc@31 301 space,
adamc@31 302 p_kind k,
adamc@31 303 space,
adamc@31 304 string "=",
adamc@31 305 space,
adamc@31 306 p_con env c]
adamc@31 307 | SgiVal (x, n, c) => box [string "val",
adamc@31 308 space,
adamc@31 309 p_named x n,
adamc@31 310 space,
adamc@31 311 string ":",
adamc@31 312 space,
adamc@31 313 p_con env c]
adamc@31 314 | SgiStr (x, n, sgn) => box [string "structure",
adamc@31 315 space,
adamc@31 316 p_named x n,
adamc@31 317 space,
adamc@31 318 string ":",
adamc@31 319 space,
adamc@31 320 p_sgn env sgn]
adamc@59 321 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@59 322 space,
adamc@59 323 p_named x n,
adamc@59 324 space,
adamc@59 325 string "=",
adamc@59 326 space,
adamc@59 327 p_sgn env sgn]
adamc@31 328
adamc@31 329 and p_sgn env (sgn, _) =
adamc@31 330 case sgn of
adamc@31 331 SgnConst sgis => box [string "sig",
adamc@31 332 newline,
adamc@32 333 let
adamc@32 334 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@32 335 (p_sgn_item env sgi,
adamc@32 336 E.sgiBinds env sgi))
adamc@32 337 env sgis
adamc@32 338 in
adamc@32 339 p_list_sep newline (fn x => x) psgis
adamc@32 340 end,
adamc@31 341 newline,
adamc@31 342 string "end"]
adamc@31 343 | SgnVar n => string (#1 (E.lookupSgnNamed env n))
adamc@41 344 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@41 345 space,
adamc@41 346 string "(",
adamc@41 347 string x,
adamc@41 348 space,
adamc@41 349 string ":",
adamc@41 350 space,
adamc@41 351 p_sgn env sgn,
adamc@41 352 string ")",
adamc@41 353 space,
adamc@41 354 string ":",
adamc@41 355 space,
adamc@41 356 p_sgn (E.pushStrNamedAs env x n sgn) sgn']
adamc@42 357 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@42 358 space,
adamc@42 359 string "where",
adamc@42 360 space,
adamc@42 361 string "con",
adamc@42 362 space,
adamc@42 363 string x,
adamc@42 364 space,
adamc@42 365 string "=",
adamc@42 366 space,
adamc@42 367 p_con env c]
adamc@59 368 | SgnProj (m1, ms, x) =>
adamc@59 369 let
adamc@59 370 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@59 371
adamc@59 372 val m1s = if !debug then
adamc@59 373 m1x ^ "__" ^ Int.toString m1
adamc@59 374 else
adamc@59 375 m1x
adamc@59 376 in
adamc@59 377 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@59 378 end
adamc@31 379 | SgnError => string "<ERROR>"
adamc@31 380
adamc@3 381 fun p_decl env ((d, _) : decl) =
adamc@3 382 case d of
adamc@31 383 DCon (x, n, k, c) => box [string "con",
adamc@31 384 space,
adamc@31 385 p_named x n,
adamc@31 386 space,
adamc@31 387 string "::",
adamc@31 388 space,
adamc@31 389 p_kind k,
adamc@31 390 space,
adamc@31 391 string "=",
adamc@31 392 space,
adamc@31 393 p_con env c]
adamc@31 394 | DVal (x, n, t, e) => box [string "val",
adamc@31 395 space,
adamc@31 396 p_named x n,
adamc@31 397 space,
adamc@31 398 string ":",
adamc@31 399 space,
adamc@31 400 p_con env t,
adamc@31 401 space,
adamc@31 402 string "=",
adamc@31 403 space,
adamc@31 404 p_exp env e]
adamc@31 405
adamc@31 406 | DSgn (x, n, sgn) => box [string "signature",
adamc@31 407 space,
adamc@31 408 p_named x n,
adamc@31 409 space,
adamc@31 410 string "=",
adamc@31 411 space,
adamc@31 412 p_sgn env sgn]
adamc@31 413 | DStr (x, n, sgn, str) => box [string "structure",
adamc@31 414 space,
adamc@31 415 p_named x n,
adamc@31 416 space,
adamc@31 417 string ":",
adamc@31 418 space,
adamc@31 419 p_sgn env sgn,
adamc@31 420 space,
adamc@31 421 string "=",
adamc@31 422 space,
adamc@31 423 p_str env str]
adamc@48 424 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 425 space,
adamc@48 426 string "structure",
adamc@48 427 space,
adamc@48 428 p_named x n,
adamc@48 429 space,
adamc@48 430 string ":",
adamc@48 431 space,
adamc@48 432 p_sgn env sgn]
adamc@31 433
adamc@31 434 and p_str env (str, _) =
adamc@31 435 case str of
adamc@31 436 StrConst ds => box [string "struct",
adamc@31 437 newline,
adamc@32 438 p_file env ds,
adamc@31 439 newline,
adamc@31 440 string "end"]
adamc@31 441 | StrVar n => string (#1 (E.lookupStrNamed env n))
adamc@34 442 | StrProj (str, s) => box [p_str env str,
adamc@34 443 string ".",
adamc@34 444 string s]
adamc@41 445 | StrFun (x, n, sgn, sgn', str) =>
adamc@41 446 let
adamc@41 447 val env' = E.pushStrNamedAs env x n sgn
adamc@41 448 in
adamc@41 449 box [string "functor",
adamc@41 450 space,
adamc@41 451 string "(",
adamc@41 452 string x,
adamc@41 453 space,
adamc@41 454 string ":",
adamc@41 455 space,
adamc@41 456 p_sgn env sgn,
adamc@41 457 string ")",
adamc@41 458 space,
adamc@41 459 string ":",
adamc@41 460 space,
adamc@41 461 p_sgn env' sgn',
adamc@41 462 space,
adamc@41 463 string "=>",
adamc@41 464 space,
adamc@41 465 p_str env' str]
adamc@41 466 end
adamc@44 467 | StrApp (str1, str2) => box [p_str env str1,
adamc@44 468 string "(",
adamc@44 469 p_str env str2,
adamc@44 470 string ")"]
adamc@31 471 | StrError => string "<ERROR>"
adamc@3 472
adamc@32 473 and p_file env file =
adamc@3 474 let
adamc@31 475 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 476 (p_decl env d,
adamc@31 477 E.declBinds env d))
adamc@31 478 env file
adamc@3 479 in
adamc@3 480 p_list_sep newline (fn x => x) pds
adamc@3 481 end
adamc@3 482
adamc@3 483 end