annotate src/cjrize.sml @ 172:021f5beb6f8d

Pattern match coverage checking
author Adam Chlipala <adamc@hcoop.net>
date Thu, 31 Jul 2008 10:31:30 -0400
parents 25b169416ea8
children eb3f9913bf31
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@29 33 structure Sm :> sig
adamc@29 34 type t
adamc@29 35
adamc@29 36 val empty : t
adamc@29 37 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
adamc@29 38
adamc@29 39 val declares : t -> (int * (string * L'.typ) list) list
adamc@29 40 end = struct
adamc@29 41
adamc@29 42 structure FM = BinaryMapFn(struct
adamc@29 43 type ord_key = L.typ
adamc@109 44 val compare = MonoUtil.Typ.compare
adamc@29 45 end)
adamc@29 46
adamc@29 47 type t = int * int FM.map * (int * (string * L'.typ) list) list
adamc@29 48
adamc@102 49 val empty : t = (1, FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0), [])
adamc@29 50
adamc@29 51 fun find ((n, m, ds), xts, xts') =
adamc@29 52 let
adamc@29 53 val t = (L.TRecord xts, ErrorMsg.dummySpan)
adamc@29 54 in
adamc@29 55 case FM.find (m, t) of
adamc@29 56 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
adamc@29 57 | SOME i => ((n, m, ds), i)
adamc@29 58 end
adamc@29 59
adamc@29 60 fun declares (_, _, ds) = ds
adamc@29 61
adamc@29 62 end
adamc@29 63
adamc@29 64 fun cifyTyp ((t, loc), sm) =
adamc@29 65 case t of
adamc@109 66 L.TFun (t1, t2) =>
adamc@29 67 let
adamc@29 68 val (t1, sm) = cifyTyp (t1, sm)
adamc@29 69 val (t2, sm) = cifyTyp (t2, sm)
adamc@29 70 in
adamc@109 71 ((L'.TFun (t1, t2), loc), sm)
adamc@29 72 end
adamc@29 73 | L.TRecord xts =>
adamc@29 74 let
adamc@29 75 val old_xts = xts
adamc@29 76 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@29 77 let
adamc@29 78 val (t, sm) = cifyTyp (t, sm)
adamc@29 79 in
adamc@29 80 ((x, t), sm)
adamc@29 81 end)
adamc@29 82 sm xts
adamc@29 83 val (sm, si) = Sm.find (sm, old_xts, xts)
adamc@29 84 in
adamc@29 85 ((L'.TRecord si, loc), sm)
adamc@29 86 end
adamc@168 87 | L.TDatatype (n, xncs) =>
adamc@168 88 let
adamc@168 89 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@168 90 case to of
adamc@168 91 NONE => ((x, n, NONE), sm)
adamc@168 92 | SOME t =>
adamc@168 93 let
adamc@168 94 val (t, sm) = cifyTyp (t, sm)
adamc@168 95 in
adamc@168 96 ((x, n, SOME t), sm)
adamc@168 97 end)
adamc@168 98 sm xncs
adamc@168 99 in
adamc@168 100 ((L'.TDatatype (n, xncs), loc), sm)
adamc@168 101 end
adamc@53 102 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
adamc@29 103
adamc@109 104 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@109 105
adamc@29 106 fun cifyExp ((e, loc), sm) =
adamc@29 107 case e of
adamc@29 108 L.EPrim p => ((L'.EPrim p, loc), sm)
adamc@29 109 | L.ERel n => ((L'.ERel n, loc), sm)
adamc@29 110 | L.ENamed n => ((L'.ENamed n, loc), sm)
adamc@53 111 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
adamc@53 112 | L.EFfiApp (m, x, es) =>
adamc@53 113 let
adamc@53 114 val (es, sm) = ListUtil.foldlMap cifyExp sm es
adamc@53 115 in
adamc@53 116 ((L'.EFfiApp (m, x, es), loc), sm)
adamc@53 117 end
adamc@29 118 | L.EApp (e1, e2) =>
adamc@29 119 let
adamc@29 120 val (e1, sm) = cifyExp (e1, sm)
adamc@29 121 val (e2, sm) = cifyExp (e2, sm)
adamc@29 122 in
adamc@29 123 ((L'.EApp (e1, e2), loc), sm)
adamc@29 124 end
adamc@109 125 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
adamc@109 126 (dummye, sm))
adamc@29 127
adamc@29 128 | L.ERecord xes =>
adamc@29 129 let
adamc@29 130 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 131
adamc@29 132 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 133 let
adamc@29 134 val (e, sm) = cifyExp (e, sm)
adamc@29 135 val (t, sm) = cifyTyp (t, sm)
adamc@29 136 in
adamc@29 137 ((x, e, t), sm)
adamc@29 138 end)
adamc@29 139 sm xes
adamc@29 140
adamc@29 141 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 142
adamc@29 143 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 144 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 145 in
adamc@29 146 ((L'.ERecord (si, xes), loc), sm)
adamc@29 147 end
adamc@29 148 | L.EField (e, x) =>
adamc@29 149 let
adamc@29 150 val (e, sm) = cifyExp (e, sm)
adamc@29 151 in
adamc@29 152 ((L'.EField (e, x), loc), sm)
adamc@29 153 end
adamc@29 154
adamc@102 155 | L.EStrcat _ => raise Fail "Cjrize EStrcat"
adamc@102 156
adamc@102 157 | L.EWrite e =>
adamc@102 158 let
adamc@102 159 val (e, sm) = cifyExp (e, sm)
adamc@102 160 in
adamc@102 161 ((L'.EWrite e, loc), sm)
adamc@102 162 end
adamc@102 163
adamc@106 164 | L.ESeq (e1, e2) =>
adamc@106 165 let
adamc@106 166 val (e1, sm) = cifyExp (e1, sm)
adamc@106 167 val (e2, sm) = cifyExp (e2, sm)
adamc@106 168 in
adamc@106 169 ((L'.ESeq (e1, e2), loc), sm)
adamc@106 170 end
adamc@106 171
adamc@111 172 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
adamc@111 173 (dummye, sm))
adamc@111 174
adamc@29 175 fun cifyDecl ((d, loc), sm) =
adamc@29 176 case d of
adamc@165 177 L.DDatatype (x, n, xncs) =>
adamc@165 178 let
adamc@165 179 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
adamc@165 180 case to of
adamc@165 181 NONE => ((x, n, NONE), sm)
adamc@165 182 | SOME t =>
adamc@165 183 let
adamc@165 184 val (t, sm) = cifyTyp (t, sm)
adamc@165 185 in
adamc@165 186 ((x, n, SOME t), sm)
adamc@165 187 end) sm xncs
adamc@165 188 in
adamc@165 189 (SOME (L'.DDatatype (x, n, xncs), loc), NONE, sm)
adamc@165 190 end
adamc@164 191
adamc@164 192 | L.DVal (x, n, t, e, _) =>
adamc@29 193 let
adamc@29 194 val (t, sm) = cifyTyp (t, sm)
adamc@109 195
adamc@109 196 val (d, sm) = case #1 t of
adamc@121 197 L'.TFun _ =>
adamc@121 198 let
adamc@121 199 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@121 200 case (t, e) of
adamc@121 201 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@121 202 let
adamc@121 203 val (args, t, e) = unravel (ran, e)
adamc@121 204 in
adamc@121 205 ((ax, dom) :: args, t, e)
adamc@121 206 end
adamc@121 207 | (L'.TFun _, _) =>
adamc@121 208 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@121 209 ([], tAll, eAll))
adamc@121 210 | _ => ([], tAll, eAll)
adamc@121 211
adamc@121 212 val (args, ran, e) = unravel (t, e)
adamc@121 213 val (e, sm) = cifyExp (e, sm)
adamc@121 214 in
adamc@121 215 (L'.DFun (x, n, args, ran, e), sm)
adamc@121 216 end
adamc@121 217
adamc@109 218 | _ =>
adamc@109 219 let
adamc@109 220 val (e, sm) = cifyExp (e, sm)
adamc@109 221 in
adamc@109 222 (L'.DVal (x, n, t, e), sm)
adamc@109 223 end
adamc@29 224 in
adamc@109 225 (SOME (d, loc), NONE, sm)
adamc@29 226 end
adamc@129 227 | L.DValRec vis =>
adamc@129 228 let
adamc@129 229 val (vis, sm) = ListUtil.foldlMap
adamc@129 230 (fn ((x, n, t, e, _), sm) =>
adamc@129 231 let
adamc@129 232 val (t, sm) = cifyTyp (t, sm)
adamc@129 233
adamc@129 234 fun unravel (tAll as (t, _), eAll as (e, _)) =
adamc@129 235 case (t, e) of
adamc@129 236 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
adamc@129 237 let
adamc@129 238 val (args, t, e) = unravel (ran, e)
adamc@129 239 in
adamc@129 240 ((ax, dom) :: args, t, e)
adamc@129 241 end
adamc@129 242 | (L'.TFun _, _) =>
adamc@129 243 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
adamc@129 244 ([], tAll, eAll))
adamc@129 245 | _ => ([], tAll, eAll)
adamc@129 246
adamc@129 247 val (args, ran, e) = unravel (t, e)
adamc@129 248 val (e, sm) = cifyExp (e, sm)
adamc@129 249 in
adamc@129 250 ((x, n, args, ran, e), sm)
adamc@129 251 end)
adamc@129 252 sm vis
adamc@129 253 in
adamc@129 254 (SOME (L'.DFunRec vis, loc), NONE, sm)
adamc@129 255 end
adamc@129 256
adamc@144 257 | L.DExport (ek, s, n, ts) =>
adamc@120 258 let
adamc@120 259 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
adamc@120 260 in
adamc@144 261 (NONE, SOME (ek, "/" ^ s, n, ts), sm)
adamc@120 262 end
adamc@29 263
adamc@29 264 fun cjrize ds =
adamc@29 265 let
adamc@101 266 val (ds, ps, sm) = foldl (fn (d, (ds, ps, sm)) =>
adamc@101 267 let
adamc@101 268 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@101 269 val ds = case dop of
adamc@101 270 NONE => ds
adamc@101 271 | SOME d => d :: ds
adamc@101 272 val ps = case pop of
adamc@101 273 NONE => ps
adamc@101 274 | SOME p => p :: ps
adamc@101 275 in
adamc@101 276 (ds, ps, sm)
adamc@101 277 end)
adamc@101 278 ([], [], Sm.empty) ds
adamc@29 279 in
adamc@101 280 (List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@101 281 rev ds),
adamc@101 282 ps)
adamc@29 283 end
adamc@29 284
adamc@29 285 end