annotate src/elab_print.sml @ 71:6431b315a1e3

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