annotate src/source_print.sml @ 175:b2d752455182

Elaborating record patterns
author Adam Chlipala <adamc@hcoop.net>
date Thu, 31 Jul 2008 13:08:57 -0400
parents 7ee424760d2f
children aa54250f58ac
rev   line source
adamc@1 1 (* Copyright (c) 2008, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 (* Pretty-printing Laconic/Web *)
adamc@1 29
adamc@4 30 structure SourcePrint :> SOURCE_PRINT = struct
adamc@1 31
adamc@1 32 open Print.PD
adamc@1 33 open Print
adamc@1 34
adamc@4 35 open Source
adamc@1 36
adamc@1 37 fun p_kind' par (k, _) =
adamc@1 38 case k of
adamc@1 39 KType => string "Type"
adamc@1 40 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@1 41 space,
adamc@1 42 string "->",
adamc@1 43 space,
adamc@1 44 p_kind k2])
adamc@1 45 | KName => string "Name"
adamc@1 46 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@82 47 | KUnit => string "Unit"
adamc@18 48 | KWild => string "_"
adamc@1 49
adamc@1 50 and p_kind k = p_kind' false k
adamc@1 51
adamc@1 52 fun p_explicitness e =
adamc@1 53 case e of
adamc@1 54 Explicit => string "::"
adamc@1 55 | Implicit => string ":::"
adamc@1 56
adamc@1 57 fun p_con' par (c, _) =
adamc@1 58 case c of
adamc@1 59 CAnnot (c, k) => box [string "(",
adamc@1 60 p_con c,
adamc@1 61 space,
adamc@1 62 string "::",
adamc@1 63 space,
adamc@1 64 p_kind k,
adamc@1 65 string ")"]
adamc@1 66
adamc@1 67 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
adamc@1 68 space,
adamc@1 69 string "->",
adamc@1 70 space,
adamc@1 71 p_con t2])
adamc@1 72 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@1 73 space,
adamc@1 74 p_explicitness e,
adamc@1 75 space,
adamc@1 76 p_kind k,
adamc@1 77 space,
adamc@1 78 string "->",
adamc@1 79 space,
adamc@1 80 p_con c])
adamc@85 81 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@85 82 space,
adamc@85 83 string "~",
adamc@85 84 space,
adamc@85 85 p_con c2,
adamc@85 86 space,
adamc@85 87 string "->",
adamc@85 88 space,
adamc@85 89 p_con c3])
adamc@1 90 | TRecord (CRecord xcs, _) => box [string "{",
adamc@1 91 p_list (fn (x, c) =>
adamc@20 92 box [p_name x,
adamc@1 93 space,
adamc@1 94 string ":",
adamc@1 95 space,
adamc@1 96 p_con c]) xcs,
adamc@1 97 string "}"]
adamc@1 98 | TRecord c => box [string "$",
adamc@1 99 p_con' true c]
adamc@1 100
adamc@34 101 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@1 102 | CApp (c1, c2) => parenIf par (box [p_con c1,
adamc@1 103 space,
adamc@1 104 p_con' true c2])
adamc@67 105 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
adamc@67 106 space,
adamc@67 107 string x,
adamc@67 108 space,
adamc@67 109 string "=>",
adamc@67 110 space,
adamc@67 111 p_con c])
adamc@67 112 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
adamc@67 113 space,
adamc@67 114 string x,
adamc@67 115 space,
adamc@67 116 string "::",
adamc@67 117 space,
adamc@67 118 p_kind k,
adamc@67 119 space,
adamc@67 120 string "=>",
adamc@67 121 space,
adamc@67 122 p_con c])
adamc@84 123 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@84 124 space,
adamc@84 125 string "~",
adamc@84 126 space,
adamc@84 127 p_con c2,
adamc@84 128 space,
adamc@84 129 string "=>",
adamc@84 130 space,
adamc@84 131 p_con c3])
adamc@1 132
adamc@1 133 | CName s => box [string "#", string s]
adamc@1 134
adamc@1 135 | CRecord xcs => box [string "[",
adamc@1 136 p_list (fn (x, c) =>
adamc@1 137 box [p_con x,
adamc@1 138 space,
adamc@1 139 string "=",
adamc@1 140 space,
adamc@1 141 p_con c]) xcs,
adamc@1 142 string "]"]
adamc@1 143 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
adamc@1 144 space,
adamc@1 145 string "++",
adamc@1 146 space,
adamc@1 147 p_con c2])
adamc@67 148 | CFold => string "fold"
adamc@82 149
adamc@82 150 | CUnit => string "()"
adamc@82 151
adamc@18 152 | CWild k => box [string "(_",
adamc@18 153 space,
adamc@18 154 string "::",
adamc@18 155 space,
adamc@18 156 p_kind k]
adamc@1 157
adamc@1 158 and p_con c = p_con' false c
adamc@1 159
adamc@20 160 and p_name (all as (c, _)) =
adamc@20 161 case c of
adamc@20 162 CName s => string s
adamc@20 163 | _ => p_con all
adamc@20 164
adamc@170 165 fun p_pat' par (p, _) =
adamc@170 166 case p of
adamc@170 167 PWild => string "_"
adamc@170 168 | PVar s => string s
adamc@173 169 | PPrim p => Prim.p_t p
adamc@170 170 | PCon (ms, x, NONE) => p_list_sep (string ".") string (ms @ [x])
adamc@170 171 | PCon (ms, x, SOME p) => parenIf par (box [p_list_sep (string ".") string (ms @ [x]),
adamc@170 172 space,
adamc@170 173 p_pat' true p])
adamc@174 174 | PRecord (xps, flex) =>
adamc@174 175 let
adamc@175 176 val pps = map (fn (x, p) => box [string x, space, string "=", space, p_pat p]) xps
adamc@174 177 in
adamc@174 178 box [string "{",
adamc@174 179 p_list_sep (box [string ",", space]) (fn x => x)
adamc@174 180 (if flex then
adamc@175 181 pps @ [string "..."]
adamc@174 182 else
adamc@175 183 pps),
adamc@174 184 string "}"]
adamc@174 185 end
adamc@170 186
adamc@174 187 and p_pat x = p_pat' false x
adamc@170 188
adamc@8 189 fun p_exp' par (e, _) =
adamc@8 190 case e of
adamc@8 191 EAnnot (e, t) => box [string "(",
adamc@8 192 p_exp e,
adamc@8 193 space,
adamc@8 194 string ":",
adamc@8 195 space,
adamc@8 196 p_con t,
adamc@8 197 string ")"]
adamc@8 198
adamc@14 199 | EPrim p => Prim.p_t p
adamc@34 200 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 201 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 202 space,
adamc@8 203 p_exp' true e2])
adamc@8 204 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 205 space,
adamc@8 206 string x,
adamc@8 207 space,
adamc@8 208 string "=>",
adamc@8 209 space,
adamc@8 210 p_exp e])
adamc@8 211 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 212 space,
adamc@8 213 string x,
adamc@8 214 space,
adamc@8 215 string ":",
adamc@8 216 space,
adamc@8 217 p_con t,
adamc@8 218 space,
adamc@8 219 string "=>",
adamc@8 220 space,
adamc@8 221 p_exp e])
adamc@8 222 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 223 space,
adamc@8 224 string "[",
adamc@8 225 p_con c,
adamc@8 226 string "]"])
adamc@8 227 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 228 space,
adamc@8 229 string x,
adamc@8 230 space,
adamc@8 231 p_explicitness exp,
adamc@8 232 space,
adamc@8 233 p_kind k,
adamc@8 234 space,
adamc@8 235 string "=>",
adamc@8 236 space,
adamc@8 237 p_exp e])
adamc@85 238 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 239 space,
adamc@85 240 string "~",
adamc@85 241 space,
adamc@85 242 p_con c2,
adamc@85 243 space,
adamc@85 244 string "=>",
adamc@85 245 space,
adamc@85 246 p_exp e])
adamc@8 247
adamc@12 248 | ERecord xes => box [string "{",
adamc@12 249 p_list (fn (x, e) =>
adamc@21 250 box [p_name x,
adamc@12 251 space,
adamc@12 252 string "=",
adamc@12 253 space,
adamc@12 254 p_exp e]) xes,
adamc@12 255 string "}"]
adamc@12 256 | EField (e, c) => box [p_exp' true e,
adamc@12 257 string ".",
adamc@12 258 p_con' true c]
adamc@149 259 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 260 space,
adamc@149 261 string "--",
adamc@149 262 space,
adamc@149 263 p_con' true c])
adamc@71 264 | EFold => string "fold"
adamc@12 265
adamc@170 266 | ECase (e, pes) => parenIf par (box [string "case",
adamc@170 267 space,
adamc@171 268 p_exp e,
adamc@170 269 space,
adamc@170 270 string "of",
adamc@170 271 space,
adamc@170 272 p_list_sep (box [space, string "|", space])
adamc@170 273 (fn (p, e) => box [p_pat p,
adamc@170 274 space,
adamc@170 275 string "=>",
adamc@170 276 space,
adamc@170 277 p_exp e]) pes])
adamc@170 278
adamc@8 279 and p_exp e = p_exp' false e
adamc@8 280
adamc@156 281 fun p_datatype (x, cons) =
adamc@156 282 box [string "datatype",
adamc@156 283 space,
adamc@156 284 string x,
adamc@156 285 space,
adamc@156 286 string "=",
adamc@156 287 space,
adamc@156 288 p_list_sep (box [space, string "|", space])
adamc@156 289 (fn (x, NONE) => string x
adamc@156 290 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 291 cons]
adamc@156 292
adamc@30 293 fun p_sgn_item (sgi, _) =
adamc@30 294 case sgi of
adamc@30 295 SgiConAbs (x, k) => box [string "con",
adamc@30 296 space,
adamc@30 297 string x,
adamc@30 298 space,
adamc@30 299 string "::",
adamc@30 300 space,
adamc@30 301 p_kind k]
adamc@30 302 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 303 space,
adamc@30 304 string x,
adamc@30 305 space,
adamc@30 306 string "=",
adamc@30 307 space,
adamc@30 308 p_con c]
adamc@30 309 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 310 space,
adamc@30 311 string x,
adamc@30 312 space,
adamc@30 313 string "::",
adamc@30 314 space,
adamc@30 315 p_kind k,
adamc@30 316 space,
adamc@30 317 string "=",
adamc@30 318 space,
adamc@30 319 p_con c]
adamc@156 320 | SgiDatatype x => p_datatype x
adamc@156 321 | SgiDatatypeImp (x, ms, x') =>
adamc@156 322 box [string "datatype",
adamc@156 323 space,
adamc@156 324 string x,
adamc@156 325 space,
adamc@156 326 string "=",
adamc@156 327 space,
adamc@156 328 string "datatype",
adamc@156 329 space,
adamc@156 330 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 331 | SgiVal (x, c) => box [string "val",
adamc@30 332 space,
adamc@30 333 string x,
adamc@30 334 space,
adamc@30 335 string ":",
adamc@30 336 space,
adamc@30 337 p_con c]
adamc@30 338 | SgiStr (x, sgn) => box [string "structure",
adamc@30 339 space,
adamc@30 340 string x,
adamc@30 341 space,
adamc@30 342 string ":",
adamc@30 343 space,
adamc@30 344 p_sgn sgn]
adamc@59 345 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 346 space,
adamc@59 347 string x,
adamc@59 348 space,
adamc@59 349 string "=",
adamc@59 350 space,
adamc@59 351 p_sgn sgn]
adamc@58 352 | SgiInclude sgn => box [string "include",
adamc@58 353 space,
adamc@58 354 p_sgn sgn]
adamc@88 355 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 356 space,
adamc@88 357 p_con c1,
adamc@88 358 space,
adamc@88 359 string "~",
adamc@88 360 space,
adamc@88 361 p_con c2]
adamc@30 362
adamc@30 363 and p_sgn (sgn, _) =
adamc@30 364 case sgn of
adamc@30 365 SgnConst sgis => box [string "sig",
adamc@30 366 newline,
adamc@30 367 p_list_sep newline p_sgn_item sgis,
adamc@30 368 newline,
adamc@30 369 string "end"]
adamc@30 370 | SgnVar x => string x
adamc@40 371 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 372 space,
adamc@40 373 string "(",
adamc@40 374 string x,
adamc@40 375 space,
adamc@40 376 string ":",
adamc@40 377 p_sgn sgn,
adamc@40 378 string ")",
adamc@40 379 space,
adamc@40 380 string ":",
adamc@40 381 space,
adamc@40 382 p_sgn sgn']
adamc@42 383 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 384 space,
adamc@42 385 string "where",
adamc@42 386 space,
adamc@42 387 string "con",
adamc@42 388 space,
adamc@42 389 string x,
adamc@42 390 space,
adamc@42 391 string "=",
adamc@42 392 space,
adamc@42 393 p_con c]
adamc@59 394 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 395
adamc@42 396
adamc@123 397 fun p_vali (x, co, e) =
adamc@123 398 case co of
adamc@123 399 NONE => box [string x,
adamc@123 400 space,
adamc@123 401 string "=",
adamc@123 402 space,
adamc@123 403 p_exp e]
adamc@123 404 | SOME t => box [string x,
adamc@123 405 space,
adamc@123 406 string ":",
adamc@123 407 space,
adamc@123 408 p_con t,
adamc@123 409 space,
adamc@123 410 string "=",
adamc@123 411 space,
adamc@123 412 p_exp e]
adamc@123 413
adamc@1 414 fun p_decl ((d, _) : decl) =
adamc@1 415 case d of
adamc@1 416 DCon (x, NONE, c) => box [string "con",
adamc@1 417 space,
adamc@1 418 string x,
adamc@1 419 space,
adamc@1 420 string "=",
adamc@1 421 space,
adamc@1 422 p_con c]
adamc@1 423 | DCon (x, SOME k, c) => box [string "con",
adamc@1 424 space,
adamc@1 425 string x,
adamc@1 426 space,
adamc@1 427 string "::",
adamc@1 428 space,
adamc@1 429 p_kind k,
adamc@1 430 space,
adamc@1 431 string "=",
adamc@1 432 space,
adamc@1 433 p_con c]
adamc@156 434 | DDatatype x => p_datatype x
adamc@156 435 | DDatatypeImp (x, ms, x') =>
adamc@156 436 box [string "datatype",
adamc@156 437 space,
adamc@156 438 string x,
adamc@156 439 space,
adamc@156 440 string "=",
adamc@156 441 space,
adamc@156 442 string "datatype",
adamc@156 443 space,
adamc@156 444 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 445 | DVal vi => box [string "val",
adamc@123 446 space,
adamc@123 447 p_vali vi]
adamc@123 448 | DValRec vis => box [string "val",
adamc@123 449 space,
adamc@123 450 string "rec",
adamc@123 451 space,
adamc@123 452 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 453
adamc@30 454 | DSgn (x, sgn) => box [string "signature",
adamc@30 455 space,
adamc@30 456 string x,
adamc@30 457 space,
adamc@30 458 string "=",
adamc@30 459 space,
adamc@30 460 p_sgn sgn]
adamc@30 461 | DStr (x, NONE, str) => box [string "structure",
adamc@30 462 space,
adamc@30 463 string x,
adamc@30 464 space,
adamc@30 465 string "=",
adamc@30 466 space,
adamc@30 467 p_str str]
adamc@30 468 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 469 space,
adamc@30 470 string x,
adamc@30 471 space,
adamc@30 472 string ":",
adamc@30 473 space,
adamc@30 474 p_sgn sgn,
adamc@30 475 space,
adamc@30 476 string "=",
adamc@30 477 space,
adamc@30 478 p_str str]
adamc@48 479 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 480 space,
adamc@48 481 string "structure",
adamc@48 482 space,
adamc@48 483 string x,
adamc@48 484 space,
adamc@48 485 string ":",
adamc@48 486 space,
adamc@48 487 p_sgn sgn]
adamc@61 488 | DOpen (m, ms) => box [string "open",
adamc@61 489 space,
adamc@61 490 p_list_sep (string ".") string (m :: ms)]
adamc@88 491 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 492 space,
adamc@88 493 p_con c1,
adamc@88 494 space,
adamc@88 495 string "~",
adamc@88 496 space,
adamc@88 497 p_con c2]
adamc@88 498 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 499 space,
adamc@88 500 string "constraints",
adamc@88 501 space,
adamc@88 502 p_list_sep (string ".") string (m :: ms)]
adamc@30 503
adamc@109 504 | DExport str => box [string "export",
adamc@109 505 space,
adamc@109 506 p_str str]
adamc@100 507
adamc@30 508 and p_str (str, _) =
adamc@30 509 case str of
adamc@30 510 StrConst ds => box [string "struct",
adamc@30 511 newline,
adamc@30 512 p_list_sep newline p_decl ds,
adamc@30 513 newline,
adamc@30 514 string "end"]
adamc@30 515 | StrVar x => string x
adamc@34 516 | StrProj (str, x) => box [p_str str,
adamc@34 517 string ".",
adamc@34 518 string x]
adamc@40 519 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 520 space,
adamc@40 521 string "(",
adamc@40 522 string x,
adamc@40 523 space,
adamc@40 524 string ":",
adamc@40 525 p_sgn sgn,
adamc@40 526 string ")",
adamc@40 527 space,
adamc@40 528 string "=>",
adamc@40 529 space,
adamc@40 530 p_str str]
adamc@40 531 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 532 space,
adamc@40 533 string "(",
adamc@40 534 string x,
adamc@40 535 space,
adamc@40 536 string ":",
adamc@40 537 p_sgn sgn,
adamc@40 538 string ")",
adamc@40 539 space,
adamc@40 540 string ":",
adamc@40 541 space,
adamc@40 542 p_sgn sgn',
adamc@40 543 space,
adamc@40 544 string "=>",
adamc@40 545 space,
adamc@40 546 p_str str]
adamc@44 547 | StrApp (str1, str2) => box [p_str str1,
adamc@44 548 string "(",
adamc@44 549 p_str str2,
adamc@44 550 string ")"]
adamc@30 551
adamc@1 552 val p_file = p_list_sep newline p_decl
adamc@1 553
adamc@1 554 end