annotate src/cjrize.sml @ 273:09c66a30ef32

Table declarations pushed to Cjr
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 13:09:54 -0400
parents 42dfb0d61cf0
children fdd7a698be01
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 structure Cjrize :> CJRIZE = struct
adamc@29 29
adamc@109 30 structure L = Mono
adamc@29 31 structure L' = Cjr
adamc@29 32
adamc@196 33 structure IM = IntBinaryMap
adamc@196 34
adamc@29 35 structure Sm :> sig
adamc@29 36 type t
adamc@29 37
adamc@29 38 val empty : t
adamc@29 39 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
adamc@29 40
adamc@29 41 val declares : t -> (int * (string * L'.typ) list) list
adamc@29 42 end = struct
adamc@29 43
adamc@29 44 structure FM = BinaryMapFn(struct
adamc@29 45 type ord_key = L.typ
adamc@109 46 val compare = MonoUtil.Typ.compare
adamc@29 47 end)
adamc@29 48
adamc@29 49 type t = int * int FM.map * (int * (string * L'.typ) list) list
adamc@29 50
adamc@102 51 val empty : t = (1, FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0), [])
adamc@29 52
adamc@29 53 fun find ((n, m, ds), xts, xts') =
adamc@29 54 let
adamc@29 55 val t = (L.TRecord xts, ErrorMsg.dummySpan)
adamc@29 56 in
adamc@29 57 case FM.find (m, t) of
adamc@29 58 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
adamc@29 59 | SOME i => ((n, m, ds), i)
adamc@29 60 end
adamc@29 61
adamc@29 62 fun declares (_, _, ds) = ds
adamc@29 63
adamc@29 64 end
adamc@29 65
adamc@196 66 fun cifyTyp x =
adamc@196 67 let
adamc@196 68 fun cify dtmap ((t, loc), sm) =
adamc@196 69 case t of
adamc@196 70 L.TFun (t1, t2) =>
adamc@196 71 let
adamc@196 72 val (t1, sm) = cify dtmap (t1, sm)
adamc@196 73 val (t2, sm) = cify dtmap (t2, sm)
adamc@196 74 in
adamc@196 75 ((L'.TFun (t1, t2), loc), sm)
adamc@196 76 end
adamc@196 77 | L.TRecord xts =>
adamc@196 78 let
adamc@196 79 val old_xts = xts
adamc@196 80 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@196 81 let
adamc@196 82 val (t, sm) = cify dtmap (t, sm)
adamc@196 83 in
adamc@196 84 ((x, t), sm)
adamc@196 85 end)
adamc@196 86 sm xts
adamc@196 87 val (sm, si) = Sm.find (sm, old_xts, xts)
adamc@196 88 in
adamc@196 89 ((L'.TRecord si, loc), sm)
adamc@196 90 end
adamc@196 91 | L.TDatatype (n, ref (dk, xncs)) =>
adamc@196 92 (case IM.find (dtmap, n) of
adamc@196 93 SOME r => ((L'.TDatatype (dk, n, r), loc), sm)
adamc@196 94 | NONE =>
adamc@196 95 let
adamc@196 96 val r = ref []
adamc@196 97 val dtmap = IM.insert (dtmap, n, r)
adamc@196 98
adamc@196 99 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@196 100 case to of
adamc@196 101 NONE => ((x, n, NONE), sm)
adamc@196 102 | SOME t =>
adamc@196 103 let
adamc@196 104 val (t, sm) = cify dtmap (t, sm)
adamc@196 105 in
adamc@196 106 ((x, n, SOME t), sm)
adamc@196 107 end)
adamc@196 108 sm xncs
adamc@196 109 in
adamc@196 110 r := xncs;
adamc@196 111 ((L'.TDatatype (dk, n, r), loc), sm)
adamc@196 112 end)
adamc@196 113 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
adamc@196 114 in
adamc@196 115 cify IM.empty x
adamc@196 116 end
adamc@29 117
adamc@109 118 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@109 119
adamc@186 120 fun cifyPatCon (pc, sm) =
adamc@181 121 case pc of
adamc@186 122 L.PConVar n => (L'.PConVar n, sm)
adamc@186 123 | L.PConFfi {mod = m, datatyp, con, arg} =>
adamc@186 124 let
adamc@186 125 val (arg, sm) =
adamc@186 126 case arg of
adamc@186 127 NONE => (NONE, sm)
adamc@186 128 | SOME t =>
adamc@186 129 let
adamc@186 130 val (t, sm) = cifyTyp (t, sm)
adamc@186 131 in
adamc@186 132 (SOME t, sm)
adamc@186 133 end
adamc@186 134 in
adamc@186 135 (L'.PConFfi {mod = m, datatyp = datatyp, con = con, arg = arg}, sm)
adamc@186 136 end
adamc@181 137
adamc@182 138 fun cifyPat ((p, loc), sm) =
adamc@181 139 case p of
adamc@182 140 L.PWild => ((L'.PWild, loc), sm)
adamc@182 141 | L.PVar (x, t) =>
adamc@182 142 let
adamc@182 143 val (t, sm) = cifyTyp (t, sm)
adamc@182 144 in
adamc@182 145 ((L'.PVar (x, t), loc), sm)
adamc@182 146 end
adamc@182 147 | L.PPrim p => ((L'.PPrim p, loc), sm)
adamc@188 148 | L.PCon (dk, pc, NONE) =>
adamc@186 149 let
adamc@186 150 val (pc, sm) = cifyPatCon (pc, sm)
adamc@186 151 in
adamc@188 152 ((L'.PCon (dk, pc, NONE), loc), sm)
adamc@186 153 end
adamc@188 154 | L.PCon (dk, pc, SOME p) =>
adamc@182 155 let
adamc@186 156 val (pc, sm) = cifyPatCon (pc, sm)
adamc@182 157 val (p, sm) = cifyPat (p, sm)
adamc@182 158 in
adamc@188 159 ((L'.PCon (dk, pc, SOME p), loc), sm)
adamc@182 160 end
adamc@182 161 | L.PRecord xps =>
adamc@182 162 let
adamc@182 163 val (xps, sm) = ListUtil.foldlMap (fn ((x, p, t), sm) =>
adamc@182 164 let
adamc@182 165 val (p, sm) = cifyPat (p, sm)
adamc@182 166 val (t, sm) = cifyTyp (t, sm)
adamc@182 167 in
adamc@182 168 ((x, p, t), sm)
adamc@182 169 end) sm xps
adamc@182 170 in
adamc@182 171 ((L'.PRecord xps, loc), sm)
adamc@182 172 end
adamc@181 173
adamc@29 174 fun cifyExp ((e, loc), sm) =
adamc@29 175 case e of
adamc@29 176 L.EPrim p => ((L'.EPrim p, loc), sm)
adamc@29 177 | L.ERel n => ((L'.ERel n, loc), sm)
adamc@29 178 | L.ENamed n => ((L'.ENamed n, loc), sm)
adamc@188 179 | L.ECon (dk, pc, eo) =>
adamc@181 180 let
adamc@181 181 val (eo, sm) =
adamc@181 182 case eo of
adamc@181 183 NONE => (NONE, sm)
adamc@181 184 | SOME e =>
adamc@181 185 let
adamc@181 186 val (e, sm) = cifyExp (e, sm)
adamc@181 187 in
adamc@181 188 (SOME e, sm)
adamc@181 189 end
adamc@186 190 val (pc, sm) = cifyPatCon (pc, sm)
adamc@181 191 in
adamc@188 192 ((L'.ECon (dk, pc, eo), loc), sm)
adamc@181 193 end
adamc@53 194 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
adamc@53 195 | L.EFfiApp (m, x, es) =>
adamc@53 196 let
adamc@53 197 val (es, sm) = ListUtil.foldlMap cifyExp sm es
adamc@53 198 in
adamc@53 199 ((L'.EFfiApp (m, x, es), loc), sm)
adamc@53 200 end
adamc@29 201 | L.EApp (e1, e2) =>
adamc@29 202 let
adamc@29 203 val (e1, sm) = cifyExp (e1, sm)
adamc@29 204 val (e2, sm) = cifyExp (e2, sm)
adamc@29 205 in
adamc@29 206 ((L'.EApp (e1, e2), loc), sm)
adamc@29 207 end
adamc@109 208 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
adamc@109 209 (dummye, sm))
adamc@29 210
adamc@29 211 | L.ERecord xes =>
adamc@29 212 let
adamc@29 213 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 214
adamc@29 215 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 216 let
adamc@29 217 val (e, sm) = cifyExp (e, sm)
adamc@29 218 val (t, sm) = cifyTyp (t, sm)
adamc@29 219 in
adamc@29 220 ((x, e, t), sm)
adamc@29 221 end)
adamc@29 222 sm xes
adamc@29 223
adamc@29 224 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 225
adamc@29 226 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 227 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 228 in
adamc@29 229 ((L'.ERecord (si, xes), loc), sm)
adamc@29 230 end
adamc@29 231 | L.EField (e, x) =>
adamc@29 232 let
adamc@29 233 val (e, sm) = cifyExp (e, sm)
adamc@29 234 in
adamc@29 235 ((L'.EField (e, x), loc), sm)
adamc@29 236 end
adamc@29 237
adamc@182 238 | L.ECase (e, pes, {disc, result}) =>
adamc@181 239 let
adamc@181 240 val (e, sm) = cifyExp (e, sm)
adamc@181 241 val (pes, sm) = ListUtil.foldlMap
adamc@181 242 (fn ((p, e), sm) =>
adamc@181 243 let
adamc@181 244 val (e, sm) = cifyExp (e, sm)
adamc@182 245 val (p, sm) = cifyPat (p, sm)
adamc@181 246 in
adamc@182 247 ((p, e), sm)
adamc@181 248 end) sm pes
adamc@182 249 val (disc, sm) = cifyTyp (disc, sm)
adamc@182 250 val (result, sm) = cifyTyp (result, sm)
adamc@181 251 in
adamc@182 252 ((L'.ECase (e, pes, {disc = disc, result = result}), loc), sm)
adamc@181 253 end
adamc@178 254
adamc@180 255 | L.EStrcat (e1, e2) =>
adamc@180 256 let
adamc@180 257 val (e1, sm) = cifyExp (e1, sm)
adamc@180 258 val (e2, sm) = cifyExp (e2, sm)
adamc@180 259 in
adamc@180 260 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
adamc@180 261 end
adamc@102 262
adamc@102 263 | L.EWrite e =>
adamc@102 264 let
adamc@102 265 val (e, sm) = cifyExp (e, sm)
adamc@102 266 in
adamc@102 267 ((L'.EWrite e, loc), sm)
adamc@102 268 end
adamc@102 269
adamc@106 270 | L.ESeq (e1, e2) =>
adamc@106 271 let
adamc@106 272 val (e1, sm) = cifyExp (e1, sm)
adamc@106 273 val (e2, sm) = cifyExp (e2, sm)
adamc@106 274 in
adamc@106 275 ((L'.ESeq (e1, e2), loc), sm)
adamc@106 276 end
adamc@106 277
adamc@269 278 | L.ELet (x, t, e1, e2) =>
adamc@269 279 let
adamc@269 280 val (t, sm) = cifyTyp (t, sm)
adamc@269 281 val (e1, sm) = cifyExp (e1, sm)
adamc@269 282 val (e2, sm) = cifyExp (e2, sm)
adamc@269 283 in
adamc@269 284 ((L'.ELet (x, t, e1, e2), loc), sm)
adamc@269 285 end
adamc@251 286
adamc@111 287 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
adamc@111 288 (dummye, sm))
adamc@111 289
adamc@269 290 | L.EQuery {exps, tables, state, query, body, initial} =>
adamc@269 291 let
adamc@269 292 val (exps', sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@269 293 let
adamc@269 294 val (t, sm) = cifyTyp (t, sm)
adamc@269 295 in
adamc@269 296 ((x, t), sm)
adamc@269 297 end) sm exps
adamc@269 298 val (tables', sm) = ListUtil.foldlMap (fn ((x, xts), sm) =>
adamc@269 299 let
adamc@269 300 val (xts, sm) = ListUtil.foldlMap
adamc@269 301 (fn ((x, t), sm) =>
adamc@269 302 let
adamc@269 303 val (t, sm) = cifyTyp (t, sm)
adamc@269 304 in
adamc@269 305 ((x, t), sm)
adamc@269 306 end) sm xts
adamc@269 307 in
adamc@269 308 ((x, xts), sm)
adamc@269 309 end) sm tables
adamc@269 310
adamc@269 311 val row = exps @ map (fn (x, xts) => (x, (L.TRecord xts, loc))) tables
adamc@269 312 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
adamc@269 313
adamc@269 314 val (tableRows, sm) = ListUtil.foldlMap (fn (((x, xts), (_, xts')), sm) =>
adamc@269 315 let
adamc@269 316 val (sm, rnum) = Sm.find (sm, xts, xts')
adamc@269 317 in
adamc@269 318 ((x, rnum), sm)
adamc@269 319 end)
adamc@269 320 sm (ListPair.zip (tables, tables'))
adamc@269 321 val row' = exps' @ map (fn (x, n) => (x, (L'.TRecord n, loc))) tableRows
adamc@269 322 val row' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row'
adamc@269 323
adamc@269 324 val (sm, rnum) = Sm.find (sm, row, row')
adamc@269 325
adamc@269 326 val (state, sm) = cifyTyp (state, sm)
adamc@269 327 val (query, sm) = cifyExp (query, sm)
adamc@269 328 val (body, sm) = cifyExp (body, sm)
adamc@269 329 val (initial, sm) = cifyExp (initial, sm)
adamc@269 330 in
adamc@269 331 ((L'.EQuery {exps = exps', tables = tables', rnum = rnum, state = state,
adamc@269 332 query = query, body = body, initial = initial}, loc), sm)
adamc@269 333 end
adamc@269 334
adamc@252 335
adamc@29 336 fun cifyDecl ((d, loc), sm) =
adamc@29 337 case d of
adamc@165 338 L.DDatatype (x, n, xncs) =>
adamc@165 339 let
adamc@198 340 val dk = ElabUtil.classifyDatatype xncs
adamc@165 341 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@165 342 case to of
adamc@165 343 NONE => ((x, n, NONE), sm)
adamc@165 344 | SOME t =>
adamc@165 345 let
adamc@165 346 val (t, sm) = cifyTyp (t, sm)
adamc@165 347 in
adamc@165 348 ((x, n, SOME t), sm)
adamc@165 349 end) sm xncs
adamc@165 350 in
adamc@188 351 (SOME (L'.DDatatype (dk, x, n, xncs), loc), NONE, sm)
adamc@165 352 end
adamc@164 353
adamc@164 354 | L.DVal (x, n, t, e, _) =>
adamc@29 355 let
adamc@29 356 val (t, sm) = cifyTyp (t, sm)
adamc@109 357
adamc@109 358 val (d, sm) = case #1 t of
adamc@121 359 L'.TFun _ =>
adamc@121 360 let
adamc@121 361 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@121 362 case (t, e) of
adamc@121 363 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@121 364 let
adamc@121 365 val (args, t, e) = unravel (ran, e)
adamc@121 366 in
adamc@121 367 ((ax, dom) :: args, t, e)
adamc@121 368 end
adamc@121 369 | (L'.TFun _, _) =>
adamc@121 370 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@121 371 ([], tAll, eAll))
adamc@121 372 | _ => ([], tAll, eAll)
adamc@121 373
adamc@121 374 val (args, ran, e) = unravel (t, e)
adamc@121 375 val (e, sm) = cifyExp (e, sm)
adamc@121 376 in
adamc@121 377 (L'.DFun (x, n, args, ran, e), sm)
adamc@121 378 end
adamc@121 379
adamc@109 380 | _ =>
adamc@109 381 let
adamc@109 382 val (e, sm) = cifyExp (e, sm)
adamc@109 383 in
adamc@109 384 (L'.DVal (x, n, t, e), sm)
adamc@109 385 end
adamc@29 386 in
adamc@109 387 (SOME (d, loc), NONE, sm)
adamc@29 388 end
adamc@129 389 | L.DValRec vis =>
adamc@129 390 let
adamc@129 391 val (vis, sm) = ListUtil.foldlMap
adamc@129 392 (fn ((x, n, t, e, _), sm) =>
adamc@129 393 let
adamc@129 394 val (t, sm) = cifyTyp (t, sm)
adamc@129 395
adamc@129 396 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@129 397 case (t, e) of
adamc@129 398 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@129 399 let
adamc@129 400 val (args, t, e) = unravel (ran, e)
adamc@129 401 in
adamc@129 402 ((ax, dom) :: args, t, e)
adamc@129 403 end
adamc@129 404 | (L'.TFun _, _) =>
adamc@129 405 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@129 406 ([], tAll, eAll))
adamc@129 407 | _ => ([], tAll, eAll)
adamc@129 408
adamc@129 409 val (args, ran, e) = unravel (t, e)
adamc@129 410 val (e, sm) = cifyExp (e, sm)
adamc@129 411 in
adamc@129 412 ((x, n, args, ran, e), sm)
adamc@129 413 end)
adamc@129 414 sm vis
adamc@129 415 in
adamc@129 416 (SOME (L'.DFunRec vis, loc), NONE, sm)
adamc@129 417 end
adamc@129 418
adamc@144 419 | L.DExport (ek, s, n, ts) =>
adamc@120 420 let
adamc@120 421 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
adamc@120 422 in
adamc@144 423 (NONE, SOME (ek, "/" ^ s, n, ts), sm)
adamc@120 424 end
adamc@29 425
adamc@273 426 | L.DTable (s, xts) =>
adamc@273 427 let
adamc@273 428 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@273 429 let
adamc@273 430 val (t, sm) = cifyTyp (t, sm)
adamc@273 431 in
adamc@273 432 ((x, t), sm)
adamc@273 433 end) sm xts
adamc@273 434 in
adamc@273 435 (SOME (L'.DTable (s, xts), loc), NONE, sm)
adamc@273 436 end
adamc@271 437 | L.DDatabase s => (SOME (L'.DDatabase s, loc), NONE, sm)
adamc@271 438
adamc@29 439 fun cjrize ds =
adamc@29 440 let
adamc@196 441 val (dsF, ds, ps, sm) = foldl (fn (d, (dsF, ds, ps, sm)) =>
adamc@196 442 let
adamc@196 443 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@196 444 val (dsF, ds) = case dop of
adamc@196 445 NONE => (dsF, ds)
adamc@196 446 | SOME (d as (L'.DDatatype (dk, x, n, _), loc)) =>
adamc@196 447 ((L'.DDatatypeForward (dk, x, n), loc) :: dsF,
adamc@196 448 d :: ds)
adamc@196 449 | SOME d => (dsF, d :: ds)
adamc@196 450 val ps = case pop of
adamc@196 451 NONE => ps
adamc@196 452 | SOME p => p :: ps
adamc@196 453 in
adamc@196 454 (dsF, ds, ps, sm)
adamc@196 455 end)
adamc@196 456 ([], [], [], Sm.empty) ds
adamc@29 457 in
adamc@196 458 (List.revAppend (dsF,
adamc@196 459 List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@196 460 rev ds)),
adamc@101 461 ps)
adamc@29 462 end
adamc@29 463
adamc@29 464 end