annotate src/elab_print.sml @ 3:daa4f1d7a663

Elaborating cons and decls
author Adam Chlipala <adamc@hcoop.net>
date Sat, 26 Jan 2008 15:26:12 -0500
parents
children 258261a53842
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@3 39 fun p_kind' par (k, _) =
adamc@3 40 case k of
adamc@3 41 KType => string "Type"
adamc@3 42 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@3 43 space,
adamc@3 44 string "->",
adamc@3 45 space,
adamc@3 46 p_kind k2])
adamc@3 47 | KName => string "Name"
adamc@3 48 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@3 49
adamc@3 50 | KError => string "<ERROR>"
adamc@3 51 | KUnif (_, ref (SOME k)) => p_kind' par k
adamc@3 52 | KUnif (s, _) => string ("<UNIF:" ^ s ^ ">")
adamc@3 53
adamc@3 54 and p_kind k = p_kind' false k
adamc@3 55
adamc@3 56 fun p_explicitness e =
adamc@3 57 case e of
adamc@3 58 Explicit => string "::"
adamc@3 59 | Implicit => string ":::"
adamc@3 60
adamc@3 61 fun p_con' par env (c, _) =
adamc@3 62 case c of
adamc@3 63 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
adamc@3 64 space,
adamc@3 65 string "->",
adamc@3 66 space,
adamc@3 67 p_con env t2])
adamc@3 68 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@3 69 space,
adamc@3 70 p_explicitness e,
adamc@3 71 space,
adamc@3 72 p_kind k,
adamc@3 73 space,
adamc@3 74 string "->",
adamc@3 75 space,
adamc@3 76 p_con (E.pushCRel env x k) c])
adamc@3 77 | TRecord (CRecord (_, xcs), _) => box [string "{",
adamc@3 78 p_list (fn (x, c) =>
adamc@3 79 box [p_con env x,
adamc@3 80 space,
adamc@3 81 string ":",
adamc@3 82 space,
adamc@3 83 p_con env c]) xcs,
adamc@3 84 string "}"]
adamc@3 85 | TRecord c => box [string "$",
adamc@3 86 p_con' true env c]
adamc@3 87
adamc@3 88 | CRel n => string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@3 89 | CNamed n => string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@3 90
adamc@3 91 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@3 92 space,
adamc@3 93 p_con' true env c2])
adamc@3 94 | CAbs (e, x, k, c) => parenIf par (box [string "fn",
adamc@3 95 space,
adamc@3 96 string x,
adamc@3 97 space,
adamc@3 98 p_explicitness e,
adamc@3 99 space,
adamc@3 100 p_kind k,
adamc@3 101 space,
adamc@3 102 string "=>",
adamc@3 103 space,
adamc@3 104 p_con (E.pushCRel env x k) c])
adamc@3 105
adamc@3 106 | CName s => box [string "#", string s]
adamc@3 107
adamc@3 108 | CRecord (k, xcs) => parenIf par (box [string "[",
adamc@3 109 p_list (fn (x, c) =>
adamc@3 110 box [p_con env x,
adamc@3 111 space,
adamc@3 112 string "=",
adamc@3 113 space,
adamc@3 114 p_con env c]) xcs,
adamc@3 115 string "]::",
adamc@3 116 p_kind k])
adamc@3 117 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@3 118 space,
adamc@3 119 string "++",
adamc@3 120 space,
adamc@3 121 p_con env c2])
adamc@3 122
adamc@3 123 | CError => string "<ERROR>"
adamc@3 124 | CUnif (_, ref (SOME c)) => p_con' par env c
adamc@3 125 | CUnif (s, _) => string ("<UNIF:" ^ s ^ ">")
adamc@3 126
adamc@3 127 and p_con env = p_con' false env
adamc@3 128
adamc@3 129 fun p_decl env ((d, _) : decl) =
adamc@3 130 case d of
adamc@3 131 DCon (x, k, c) => box [string "con",
adamc@3 132 space,
adamc@3 133 string x,
adamc@3 134 space,
adamc@3 135 string "::",
adamc@3 136 space,
adamc@3 137 p_kind k,
adamc@3 138 space,
adamc@3 139 string "=",
adamc@3 140 space,
adamc@3 141 p_con env c]
adamc@3 142
adamc@3 143 fun p_file env file =
adamc@3 144 let
adamc@3 145 val (_, pds) = foldr (fn (d, (env, pds)) =>
adamc@3 146 (ElabUtil.declBinds env d,
adamc@3 147 p_decl env d :: pds))
adamc@3 148 (env, []) file
adamc@3 149 in
adamc@3 150 p_list_sep newline (fn x => x) pds
adamc@3 151 end
adamc@3 152
adamc@3 153 end