annotate src/elab_print.sml @ 61:48b6d2c3df46

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