comparison 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
comparison
equal deleted inserted replaced
2:64f09f7822c3 3:daa4f1d7a663
1 (* Copyright (c) 2008, Adam Chlipala
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - The names of contributors may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *)
27
28 (* Pretty-printing elaborated Laconic/Web *)
29
30 structure ElabPrint :> ELAB_PRINT = struct
31
32 open Print.PD
33 open Print
34
35 open Elab
36
37 structure E = ElabEnv
38
39 fun p_kind' par (k, _) =
40 case k of
41 KType => string "Type"
42 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
43 space,
44 string "->",
45 space,
46 p_kind k2])
47 | KName => string "Name"
48 | KRecord k => box [string "{", p_kind k, string "}"]
49
50 | KError => string "<ERROR>"
51 | KUnif (_, ref (SOME k)) => p_kind' par k
52 | KUnif (s, _) => string ("<UNIF:" ^ s ^ ">")
53
54 and p_kind k = p_kind' false k
55
56 fun p_explicitness e =
57 case e of
58 Explicit => string "::"
59 | Implicit => string ":::"
60
61 fun p_con' par env (c, _) =
62 case c of
63 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
64 space,
65 string "->",
66 space,
67 p_con env t2])
68 | TCFun (e, x, k, c) => parenIf par (box [string x,
69 space,
70 p_explicitness e,
71 space,
72 p_kind k,
73 space,
74 string "->",
75 space,
76 p_con (E.pushCRel env x k) c])
77 | TRecord (CRecord (_, xcs), _) => box [string "{",
78 p_list (fn (x, c) =>
79 box [p_con env x,
80 space,
81 string ":",
82 space,
83 p_con env c]) xcs,
84 string "}"]
85 | TRecord c => box [string "$",
86 p_con' true env c]
87
88 | CRel n => string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
89 | CNamed n => string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
90
91 | CApp (c1, c2) => parenIf par (box [p_con env c1,
92 space,
93 p_con' true env c2])
94 | CAbs (e, x, k, c) => parenIf par (box [string "fn",
95 space,
96 string x,
97 space,
98 p_explicitness e,
99 space,
100 p_kind k,
101 space,
102 string "=>",
103 space,
104 p_con (E.pushCRel env x k) c])
105
106 | CName s => box [string "#", string s]
107
108 | CRecord (k, xcs) => parenIf par (box [string "[",
109 p_list (fn (x, c) =>
110 box [p_con env x,
111 space,
112 string "=",
113 space,
114 p_con env c]) xcs,
115 string "]::",
116 p_kind k])
117 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
118 space,
119 string "++",
120 space,
121 p_con env c2])
122
123 | CError => string "<ERROR>"
124 | CUnif (_, ref (SOME c)) => p_con' par env c
125 | CUnif (s, _) => string ("<UNIF:" ^ s ^ ">")
126
127 and p_con env = p_con' false env
128
129 fun p_decl env ((d, _) : decl) =
130 case d of
131 DCon (x, k, c) => box [string "con",
132 space,
133 string x,
134 space,
135 string "::",
136 space,
137 p_kind k,
138 space,
139 string "=",
140 space,
141 p_con env c]
142
143 fun p_file env file =
144 let
145 val (_, pds) = foldr (fn (d, (env, pds)) =>
146 (ElabUtil.declBinds env d,
147 p_decl env d :: pds))
148 (env, []) file
149 in
150 p_list_sep newline (fn x => x) pds
151 end
152
153 end