annotate src/elab_print.sml @ 11:e97c6d335869

Simple elaboration working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 28 Mar 2008 15:20:46 -0400
parents 14b533dbe6cc
children d89477f07c1e
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@3 81 box [p_con 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@11 96 if !debug then
adamc@11 97 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@11 98 else
adamc@11 99 string (#1 (E.lookupCNamed env n))
adamc@3 100
adamc@3 101 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@3 102 space,
adamc@3 103 p_con' true env c2])
adamc@8 104 | CAbs (x, k, c) => parenIf par (box [string "fn",
adamc@8 105 space,
adamc@8 106 string x,
adamc@8 107 space,
adamc@8 108 string "::",
adamc@8 109 space,
adamc@8 110 p_kind k,
adamc@8 111 space,
adamc@8 112 string "=>",
adamc@8 113 space,
adamc@8 114 p_con (E.pushCRel env x k) c])
adamc@3 115
adamc@3 116 | CName s => box [string "#", string s]
adamc@3 117
adamc@3 118 | CRecord (k, xcs) => parenIf par (box [string "[",
adamc@3 119 p_list (fn (x, c) =>
adamc@3 120 box [p_con env x,
adamc@3 121 space,
adamc@3 122 string "=",
adamc@3 123 space,
adamc@3 124 p_con env c]) xcs,
adamc@3 125 string "]::",
adamc@3 126 p_kind k])
adamc@3 127 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@3 128 space,
adamc@3 129 string "++",
adamc@3 130 space,
adamc@3 131 p_con env c2])
adamc@3 132
adamc@3 133 | CError => string "<ERROR>"
adamc@6 134 | CUnif (_, _, ref (SOME c)) => p_con' par env c
adamc@6 135 | CUnif (k, s, _) => box [string ("<UNIF:" ^ s ^ "::"),
adamc@6 136 p_kind k,
adamc@6 137 string ">"]
adamc@3 138
adamc@3 139 and p_con env = p_con' false env
adamc@3 140
adamc@9 141 fun p_exp' par env (e, _) =
adamc@9 142 case e of
adamc@11 143 ERel n =>
adamc@11 144 if !debug then
adamc@11 145 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@11 146 else
adamc@11 147 string (#1 (E.lookupERel env n))
adamc@11 148 | ENamed n =>
adamc@11 149 if !debug then
adamc@11 150 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@11 151 else
adamc@11 152 string (#1 (E.lookupENamed env n))
adamc@9 153 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@9 154 space,
adamc@9 155 p_exp' true env e2])
adamc@9 156 | EAbs (x, t, e) => parenIf par (box [string "fn",
adamc@9 157 space,
adamc@9 158 string x,
adamc@9 159 space,
adamc@9 160 string ":",
adamc@9 161 space,
adamc@9 162 p_con env t,
adamc@9 163 space,
adamc@9 164 string "=>",
adamc@9 165 space,
adamc@9 166 p_exp (E.pushERel env x t) e])
adamc@9 167 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@9 168 space,
adamc@9 169 string "[",
adamc@9 170 p_con env c,
adamc@9 171 string "]"])
adamc@9 172 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@9 173 space,
adamc@9 174 string x,
adamc@9 175 space,
adamc@9 176 p_explicitness exp,
adamc@9 177 space,
adamc@9 178 p_kind k,
adamc@9 179 space,
adamc@9 180 string "=>",
adamc@9 181 space,
adamc@9 182 p_exp (E.pushCRel env x k) e])
adamc@9 183
adamc@9 184 | EError => string "<ERROR>"
adamc@9 185
adamc@9 186 and p_exp env = p_exp' false env
adamc@9 187
adamc@3 188 fun p_decl env ((d, _) : decl) =
adamc@3 189 case d of
adamc@11 190 DCon (x, n, k, c) =>
adamc@11 191 let
adamc@11 192 val xp = if !debug then
adamc@11 193 box [string x,
adamc@11 194 string "__",
adamc@11 195 string (Int.toString n)]
adamc@11 196 else
adamc@11 197 string x
adamc@11 198 in
adamc@11 199 box [string "con",
adamc@11 200 space,
adamc@11 201 xp,
adamc@11 202 space,
adamc@11 203 string "::",
adamc@11 204 space,
adamc@11 205 p_kind k,
adamc@11 206 space,
adamc@11 207 string "=",
adamc@11 208 space,
adamc@11 209 p_con env c]
adamc@11 210 end
adamc@11 211 | DVal (x, n, t, e) =>
adamc@11 212 let
adamc@11 213 val xp = if !debug then
adamc@11 214 box [string x,
adamc@11 215 string "__",
adamc@11 216 string (Int.toString n)]
adamc@11 217 else
adamc@11 218 string x
adamc@11 219 in
adamc@11 220 box [string "val",
adamc@11 221 space,
adamc@11 222 xp,
adamc@11 223 space,
adamc@11 224 string ":",
adamc@11 225 space,
adamc@11 226 p_con env t,
adamc@11 227 space,
adamc@11 228 string "=",
adamc@11 229 space,
adamc@11 230 p_exp env e]
adamc@11 231 end
adamc@3 232
adamc@3 233 fun p_file env file =
adamc@3 234 let
adamc@5 235 val (_, pds) = ListUtil.mapfoldl (fn (d, env) =>
adamc@5 236 (ElabUtil.declBinds env d,
adamc@5 237 p_decl env d))
adamc@5 238 env file
adamc@3 239 in
adamc@3 240 p_list_sep newline (fn x => x) pds
adamc@3 241 end
adamc@3 242
adamc@3 243 end