comparison src/corify.sml @ 39:02f42e9a1825

Corify removes modules
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Jun 2008 12:39:22 -0400
parents 44b5405e74c7
children 3c1ce1b4eb3d
comparison
equal deleted inserted replaced
38:d16ef24de78b 39:02f42e9a1825
26 *) 26 *)
27 27
28 structure Corify :> CORIFY = struct 28 structure Corify :> CORIFY = struct
29 29
30 structure EM = ErrorMsg 30 structure EM = ErrorMsg
31 structure L = Elab 31 structure L = Expl
32 structure L' = Core 32 structure L' = Core
33
34 structure IM = IntBinaryMap
35 structure SM = BinaryMapFn(struct
36 type ord_key = string
37 val compare = String.compare
38 end)
39
40 local
41 val count = ref 0
42 in
43
44 fun reset v = count := v
45
46 fun alloc () =
47 let
48 val r = !count
49 in
50 count := r + 1;
51 r
52 end
53
54 end
55
56 structure St : sig
57 type t
58
59 val empty : t
60
61 val enter : t -> t
62 val leave : t -> {outer : t, inner : t}
63
64 val bindCore : t -> string -> int -> t * int
65 val lookupCoreById : t -> int -> int option
66 val lookupCoreByName : t -> string -> int
67
68 val bindStr : t -> string -> int -> t -> t
69 val lookupStrById : t -> int -> t
70 val lookupStrByName : string * t -> t
71 end = struct
72
73 datatype flattening = F of {
74 core : int SM.map,
75 strs : flattening SM.map
76 }
77
78 type t = {
79 core : int IM.map,
80 strs : flattening IM.map,
81 current : flattening,
82 nested : flattening list
83 }
84
85 val empty = {
86 core = IM.empty,
87 strs = IM.empty,
88 current = F { core = SM.empty, strs = SM.empty },
89 nested = []
90 }
91
92 fun bindCore {core, strs, current, nested} s n =
93 let
94 val n' = alloc ()
95
96 val current =
97 let
98 val F {core, strs} = current
99 in
100 F {core = SM.insert (core, s, n'),
101 strs = strs}
102 end
103 in
104 ({core = IM.insert (core, n, n'),
105 strs = strs,
106 current = current,
107 nested = nested},
108 n')
109 end
110
111 fun lookupCoreById ({core, ...} : t) n = IM.find (core, n)
112
113 fun lookupCoreByName ({current = F {core, ...}, ...} : t) x =
114 case SM.find (core, x) of
115 NONE => raise Fail "Corify.St.lookupCoreByName"
116 | SOME n => n
117
118 fun enter {core, strs, current, nested} =
119 {core = core,
120 strs = strs,
121 current = F {core = SM.empty,
122 strs = SM.empty},
123 nested = current :: nested}
124
125 fun dummy f = {core = IM.empty,
126 strs = IM.empty,
127 current = f,
128 nested = []}
129
130 fun leave {core, strs, current, nested = m1 :: rest} =
131 {outer = {core = core,
132 strs = strs,
133 current = m1,
134 nested = rest},
135 inner = dummy current}
136 | leave _ = raise Fail "Corify.St.leave"
137
138 fun bindStr ({core, strs, current = F {core = mcore, strs = mstrs}, nested} : t) x n ({current = f, ...} : t) =
139 {core = core,
140 strs = IM.insert (strs, n, f),
141 current = F {core = mcore,
142 strs = SM.insert (mstrs, x, f)},
143 nested = nested}
144
145 fun lookupStrById ({strs, ...} : t) n =
146 case IM.find (strs, n) of
147 NONE => raise Fail "Corify.St.lookupStr"
148 | SOME f => dummy f
149
150 fun lookupStrByName (m, {current = F {strs, ...}, ...} : t) =
151 case SM.find (strs, m) of
152 NONE => raise Fail "Corify.St.lookupStrByName"
153 | SOME f => dummy f
154
155 end
156
33 157
34 fun corifyKind (k, loc) = 158 fun corifyKind (k, loc) =
35 case k of 159 case k of
36 L.KType => (L'.KType, loc) 160 L.KType => (L'.KType, loc)
37 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc) 161 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
38 | L.KName => (L'.KName, loc) 162 | L.KName => (L'.KName, loc)
39 | L.KRecord k => (L'.KRecord (corifyKind k), loc) 163 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
40 164
41 | L.KError => raise Fail ("corifyKind: KError at " ^ EM.spanToString loc) 165 fun corifyCon st (c, loc) =
42 | L.KUnif (_, ref (SOME k)) => corifyKind k
43 | L.KUnif _ => raise Fail ("corifyKind: KUnif at " ^ EM.spanToString loc)
44
45 fun corifyCon (c, loc) =
46 case c of 166 case c of
47 L.TFun (t1, t2) => (L'.TFun (corifyCon t1, corifyCon t2), loc) 167 L.TFun (t1, t2) => (L'.TFun (corifyCon st t1, corifyCon st t2), loc)
48 | L.TCFun (_, x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon t), loc) 168 | L.TCFun (x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon st t), loc)
49 | L.TRecord c => (L'.TRecord (corifyCon c), loc) 169 | L.TRecord c => (L'.TRecord (corifyCon st c), loc)
50 170
51 | L.CRel n => (L'.CRel n, loc) 171 | L.CRel n => (L'.CRel n, loc)
52 | L.CNamed n => (L'.CNamed n, loc) 172 | L.CNamed n =>
53 | L.CModProj _ => raise Fail "Corify CModProj" 173 (case St.lookupCoreById st n of
54 174 NONE => (L'.CNamed n, loc)
55 | L.CApp (c1, c2) => (L'.CApp (corifyCon c1, corifyCon c2), loc) 175 | SOME n => (L'.CNamed n, loc))
56 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon c), loc) 176 | L.CModProj (m, ms, x) =>
177 let
178 val st = St.lookupStrById st m
179 val st = foldl St.lookupStrByName st ms
180 val n = St.lookupCoreByName st x
181 in
182 (L'.CNamed n, loc)
183 end
184
185 | L.CApp (c1, c2) => (L'.CApp (corifyCon st c1, corifyCon st c2), loc)
186 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon st c), loc)
57 187
58 | L.CName s => (L'.CName s, loc) 188 | L.CName s => (L'.CName s, loc)
59 189
60 | L.CRecord (k, xcs) => (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon c1, corifyCon c2)) xcs), loc) 190 | L.CRecord (k, xcs) =>
61 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon c1, corifyCon c2), loc) 191 (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon st c1, corifyCon st c2)) xcs), loc)
62 192 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon st c1, corifyCon st c2), loc)
63 | L.CError => raise Fail ("corifyCon: CError at " ^ EM.spanToString loc) 193
64 | L.CUnif (_, _, ref (SOME c)) => corifyCon c 194 fun corifyExp st (e, loc) =
65 | L.CUnif _ => raise Fail ("corifyCon: CUnif at " ^ EM.spanToString loc)
66
67 fun corifyExp (e, loc) =
68 case e of 195 case e of
69 L.EPrim p => (L'.EPrim p, loc) 196 L.EPrim p => (L'.EPrim p, loc)
70 | L.ERel n => (L'.ERel n, loc) 197 | L.ERel n => (L'.ERel n, loc)
71 | L.ENamed n => (L'.ENamed n, loc) 198 | L.ENamed n =>
72 | L.EModProj _ => raise Fail "Corify EModProj" 199 (case St.lookupCoreById st n of
73 | L.EApp (e1, e2) => (L'.EApp (corifyExp e1, corifyExp e2), loc) 200 NONE => (L'.ENamed n, loc)
74 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon dom, corifyCon ran, corifyExp e1), loc) 201 | SOME n => (L'.ENamed n, loc))
75 | L.ECApp (e1, c) => (L'.ECApp (corifyExp e1, corifyCon c), loc) 202 | L.EModProj (m, ms, x) =>
76 | L.ECAbs (_, x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp e1), loc) 203 let
77 204 val st = St.lookupStrById st m
78 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon c, corifyExp e, corifyCon t)) xes), loc) 205 val st = foldl St.lookupStrByName st ms
79 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp e1, corifyCon c, 206 val n = St.lookupCoreByName st x
80 {field = corifyCon field, rest = corifyCon rest}), loc) 207 in
81 208 (L'.ENamed n, loc)
82 | L.EError => raise Fail ("corifyExp: EError at " ^ EM.spanToString loc) 209 end
83 210 | L.EApp (e1, e2) => (L'.EApp (corifyExp st e1, corifyExp st e2), loc)
84 fun corifyDecl (d, loc : EM.span) = 211 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon st dom, corifyCon st ran, corifyExp st e1), loc)
212 | L.ECApp (e1, c) => (L'.ECApp (corifyExp st e1, corifyCon st c), loc)
213 | L.ECAbs (x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp st e1), loc)
214
215 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon st c, corifyExp st e, corifyCon st t)) xes), loc)
216 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp st e1, corifyCon st c,
217 {field = corifyCon st field, rest = corifyCon st rest}), loc)
218
219 fun corifyDecl ((d, loc : EM.span), st) =
85 case d of 220 case d of
86 L.DCon (x, n, k, c) => (L'.DCon (x, n, corifyKind k, corifyCon c), loc) 221 L.DCon (x, n, k, c) =>
87 | L.DVal (x, n, t, e) => (L'.DVal (x, n, corifyCon t, corifyExp e), loc) 222 let
88 223 val (st, n) = St.bindCore st x n
89 | L.DSgn _ => raise Fail "Not ready to corify signature" 224 in
90 | L.DStr _ => raise Fail "Not ready to corify structure" 225 ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st)
91 226 end
92 val corify = map corifyDecl 227 | L.DVal (x, n, t, e) =>
93 228 let
94 end 229 val (st, n) = St.bindCore st x n
230 in
231 ([(L'.DVal (x, n, corifyCon st t, corifyExp st e), loc)], st)
232 end
233
234 | L.DSgn _ => ([], st)
235
236 | L.DStr (x, n, _, str) =>
237 let
238 val (ds, {inner, outer}) = corifyStr (str, st)
239 val st = St.bindStr outer x n inner
240 in
241 (ds, st)
242 end
243
244 and corifyStr ((str, _), st) =
245 case str of
246 L.StrConst ds =>
247 let
248 val st = St.enter st
249 val (ds, st) = ListUtil.foldlMapConcat corifyDecl st ds
250 in
251 (ds, St.leave st)
252 end
253 | L.StrVar n => ([], {inner = St.lookupStrById st n, outer = st})
254 | L.StrProj (str, x) =>
255 let
256 val (ds, {inner, outer}) = corifyStr (str, st)
257 in
258 (ds, {inner = St.lookupStrByName (x, inner), outer = outer})
259 end
260
261 fun maxName ds = foldl (fn ((d, _), n) =>
262 case d of
263 L.DCon (_, n', _, _) => Int.max (n, n')
264 | L.DVal (_, n', _ , _) => Int.max (n, n')
265 | L.DSgn (_, n', _) => Int.max (n, n')
266 | L.DStr (_, n', _, str) => Int.max (n, Int.max (n', maxNameStr str)))
267 0 ds
268
269 and maxNameStr (str, _) =
270 case str of
271 L.StrConst ds => maxName ds
272 | L.StrVar n => n
273 | L.StrProj (str, _) => maxNameStr str
274
275 fun corify ds =
276 let
277 val () = reset (maxName ds + 1)
278 val (ds, _) = ListUtil.foldlMapConcat corifyDecl St.empty ds
279 in
280 ds
281 end
282
283 end