annotate src/cjrize.sml @ 280:fdd7a698be01

Compiling a parametrized query the inefficient way
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 17:31:45 -0400
parents 09c66a30ef32
children 0236d9412ad2
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@280 174 fun cifyExp (eAll as (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@280 209 Print.prefaces' [("Function", MonoPrint.p_exp MonoEnv.empty eAll)];
adamc@109 210 (dummye, sm))
adamc@29 211
adamc@29 212 | L.ERecord xes =>
adamc@29 213 let
adamc@29 214 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 215
adamc@29 216 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 217 let
adamc@29 218 val (e, sm) = cifyExp (e, sm)
adamc@29 219 val (t, sm) = cifyTyp (t, sm)
adamc@29 220 in
adamc@29 221 ((x, e, t), sm)
adamc@29 222 end)
adamc@29 223 sm xes
adamc@29 224
adamc@29 225 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 226
adamc@29 227 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 228 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 229 in
adamc@29 230 ((L'.ERecord (si, xes), loc), sm)
adamc@29 231 end
adamc@29 232 | L.EField (e, x) =>
adamc@29 233 let
adamc@29 234 val (e, sm) = cifyExp (e, sm)
adamc@29 235 in
adamc@29 236 ((L'.EField (e, x), loc), sm)
adamc@29 237 end
adamc@29 238
adamc@182 239 | L.ECase (e, pes, {disc, result}) =>
adamc@181 240 let
adamc@181 241 val (e, sm) = cifyExp (e, sm)
adamc@181 242 val (pes, sm) = ListUtil.foldlMap
adamc@181 243 (fn ((p, e), sm) =>
adamc@181 244 let
adamc@181 245 val (e, sm) = cifyExp (e, sm)
adamc@182 246 val (p, sm) = cifyPat (p, sm)
adamc@181 247 in
adamc@182 248 ((p, e), sm)
adamc@181 249 end) sm pes
adamc@182 250 val (disc, sm) = cifyTyp (disc, sm)
adamc@182 251 val (result, sm) = cifyTyp (result, sm)
adamc@181 252 in
adamc@182 253 ((L'.ECase (e, pes, {disc = disc, result = result}), loc), sm)
adamc@181 254 end
adamc@178 255
adamc@180 256 | L.EStrcat (e1, e2) =>
adamc@180 257 let
adamc@180 258 val (e1, sm) = cifyExp (e1, sm)
adamc@180 259 val (e2, sm) = cifyExp (e2, sm)
adamc@180 260 in
adamc@180 261 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
adamc@180 262 end
adamc@102 263
adamc@102 264 | L.EWrite e =>
adamc@102 265 let
adamc@102 266 val (e, sm) = cifyExp (e, sm)
adamc@102 267 in
adamc@102 268 ((L'.EWrite e, loc), sm)
adamc@102 269 end
adamc@102 270
adamc@106 271 | L.ESeq (e1, e2) =>
adamc@106 272 let
adamc@106 273 val (e1, sm) = cifyExp (e1, sm)
adamc@106 274 val (e2, sm) = cifyExp (e2, sm)
adamc@106 275 in
adamc@106 276 ((L'.ESeq (e1, e2), loc), sm)
adamc@106 277 end
adamc@106 278
adamc@269 279 | L.ELet (x, t, e1, e2) =>
adamc@269 280 let
adamc@269 281 val (t, sm) = cifyTyp (t, sm)
adamc@269 282 val (e1, sm) = cifyExp (e1, sm)
adamc@269 283 val (e2, sm) = cifyExp (e2, sm)
adamc@269 284 in
adamc@269 285 ((L'.ELet (x, t, e1, e2), loc), sm)
adamc@269 286 end
adamc@251 287
adamc@111 288 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
adamc@111 289 (dummye, sm))
adamc@111 290
adamc@269 291 | L.EQuery {exps, tables, state, query, body, initial} =>
adamc@269 292 let
adamc@269 293 val (exps', sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@269 294 let
adamc@269 295 val (t, sm) = cifyTyp (t, sm)
adamc@269 296 in
adamc@269 297 ((x, t), sm)
adamc@269 298 end) sm exps
adamc@269 299 val (tables', sm) = ListUtil.foldlMap (fn ((x, xts), sm) =>
adamc@269 300 let
adamc@269 301 val (xts, sm) = ListUtil.foldlMap
adamc@269 302 (fn ((x, t), sm) =>
adamc@269 303 let
adamc@269 304 val (t, sm) = cifyTyp (t, sm)
adamc@269 305 in
adamc@269 306 ((x, t), sm)
adamc@269 307 end) sm xts
adamc@269 308 in
adamc@269 309 ((x, xts), sm)
adamc@269 310 end) sm tables
adamc@269 311
adamc@269 312 val row = exps @ map (fn (x, xts) => (x, (L.TRecord xts, loc))) tables
adamc@269 313 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
adamc@269 314
adamc@269 315 val (tableRows, sm) = ListUtil.foldlMap (fn (((x, xts), (_, xts')), sm) =>
adamc@269 316 let
adamc@269 317 val (sm, rnum) = Sm.find (sm, xts, xts')
adamc@269 318 in
adamc@269 319 ((x, rnum), sm)
adamc@269 320 end)
adamc@269 321 sm (ListPair.zip (tables, tables'))
adamc@269 322 val row' = exps' @ map (fn (x, n) => (x, (L'.TRecord n, loc))) tableRows
adamc@269 323 val row' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row'
adamc@269 324
adamc@269 325 val (sm, rnum) = Sm.find (sm, row, row')
adamc@269 326
adamc@269 327 val (state, sm) = cifyTyp (state, sm)
adamc@269 328 val (query, sm) = cifyExp (query, sm)
adamc@269 329 val (body, sm) = cifyExp (body, sm)
adamc@269 330 val (initial, sm) = cifyExp (initial, sm)
adamc@269 331 in
adamc@269 332 ((L'.EQuery {exps = exps', tables = tables', rnum = rnum, state = state,
adamc@269 333 query = query, body = body, initial = initial}, loc), sm)
adamc@269 334 end
adamc@269 335
adamc@252 336
adamc@29 337 fun cifyDecl ((d, loc), sm) =
adamc@29 338 case d of
adamc@165 339 L.DDatatype (x, n, xncs) =>
adamc@165 340 let
adamc@198 341 val dk = ElabUtil.classifyDatatype xncs
adamc@165 342 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@165 343 case to of
adamc@165 344 NONE => ((x, n, NONE), sm)
adamc@165 345 | SOME t =>
adamc@165 346 let
adamc@165 347 val (t, sm) = cifyTyp (t, sm)
adamc@165 348 in
adamc@165 349 ((x, n, SOME t), sm)
adamc@165 350 end) sm xncs
adamc@165 351 in
adamc@188 352 (SOME (L'.DDatatype (dk, x, n, xncs), loc), NONE, sm)
adamc@165 353 end
adamc@164 354
adamc@164 355 | L.DVal (x, n, t, e, _) =>
adamc@29 356 let
adamc@29 357 val (t, sm) = cifyTyp (t, sm)
adamc@109 358
adamc@109 359 val (d, sm) = case #1 t of
adamc@121 360 L'.TFun _ =>
adamc@121 361 let
adamc@121 362 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@121 363 case (t, e) of
adamc@121 364 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@121 365 let
adamc@121 366 val (args, t, e) = unravel (ran, e)
adamc@121 367 in
adamc@121 368 ((ax, dom) :: args, t, e)
adamc@121 369 end
adamc@121 370 | (L'.TFun _, _) =>
adamc@121 371 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@121 372 ([], tAll, eAll))
adamc@121 373 | _ => ([], tAll, eAll)
adamc@121 374
adamc@121 375 val (args, ran, e) = unravel (t, e)
adamc@121 376 val (e, sm) = cifyExp (e, sm)
adamc@121 377 in
adamc@121 378 (L'.DFun (x, n, args, ran, e), sm)
adamc@121 379 end
adamc@121 380
adamc@109 381 | _ =>
adamc@109 382 let
adamc@109 383 val (e, sm) = cifyExp (e, sm)
adamc@109 384 in
adamc@109 385 (L'.DVal (x, n, t, e), sm)
adamc@109 386 end
adamc@29 387 in
adamc@109 388 (SOME (d, loc), NONE, sm)
adamc@29 389 end
adamc@129 390 | L.DValRec vis =>
adamc@129 391 let
adamc@129 392 val (vis, sm) = ListUtil.foldlMap
adamc@129 393 (fn ((x, n, t, e, _), sm) =>
adamc@129 394 let
adamc@129 395 val (t, sm) = cifyTyp (t, sm)
adamc@129 396
adamc@129 397 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@129 398 case (t, e) of
adamc@129 399 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@129 400 let
adamc@129 401 val (args, t, e) = unravel (ran, e)
adamc@129 402 in
adamc@129 403 ((ax, dom) :: args, t, e)
adamc@129 404 end
adamc@129 405 | (L'.TFun _, _) =>
adamc@129 406 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@129 407 ([], tAll, eAll))
adamc@129 408 | _ => ([], tAll, eAll)
adamc@129 409
adamc@129 410 val (args, ran, e) = unravel (t, e)
adamc@129 411 val (e, sm) = cifyExp (e, sm)
adamc@129 412 in
adamc@129 413 ((x, n, args, ran, e), sm)
adamc@129 414 end)
adamc@129 415 sm vis
adamc@129 416 in
adamc@129 417 (SOME (L'.DFunRec vis, loc), NONE, sm)
adamc@129 418 end
adamc@129 419
adamc@144 420 | L.DExport (ek, s, n, ts) =>
adamc@120 421 let
adamc@120 422 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
adamc@120 423 in
adamc@144 424 (NONE, SOME (ek, "/" ^ s, n, ts), sm)
adamc@120 425 end
adamc@29 426
adamc@273 427 | L.DTable (s, xts) =>
adamc@273 428 let
adamc@273 429 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@273 430 let
adamc@273 431 val (t, sm) = cifyTyp (t, sm)
adamc@273 432 in
adamc@273 433 ((x, t), sm)
adamc@273 434 end) sm xts
adamc@273 435 in
adamc@273 436 (SOME (L'.DTable (s, xts), loc), NONE, sm)
adamc@273 437 end
adamc@271 438 | L.DDatabase s => (SOME (L'.DDatabase s, loc), NONE, sm)
adamc@271 439
adamc@29 440 fun cjrize ds =
adamc@29 441 let
adamc@196 442 val (dsF, ds, ps, sm) = foldl (fn (d, (dsF, ds, ps, sm)) =>
adamc@196 443 let
adamc@196 444 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@196 445 val (dsF, ds) = case dop of
adamc@196 446 NONE => (dsF, ds)
adamc@196 447 | SOME (d as (L'.DDatatype (dk, x, n, _), loc)) =>
adamc@196 448 ((L'.DDatatypeForward (dk, x, n), loc) :: dsF,
adamc@196 449 d :: ds)
adamc@196 450 | SOME d => (dsF, d :: ds)
adamc@196 451 val ps = case pop of
adamc@196 452 NONE => ps
adamc@196 453 | SOME p => p :: ps
adamc@196 454 in
adamc@196 455 (dsF, ds, ps, sm)
adamc@196 456 end)
adamc@196 457 ([], [], [], Sm.empty) ds
adamc@29 458 in
adamc@196 459 (List.revAppend (dsF,
adamc@196 460 List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@196 461 rev ds)),
adamc@101 462 ps)
adamc@29 463 end
adamc@29 464
adamc@29 465 end