annotate src/mono_util.sml @ 106:d101cb1efe55

More with attributes and efficient serialization
author Adam Chlipala <adamc@hcoop.net>
date Thu, 10 Jul 2008 15:49:14 -0400
parents 5f04adf47f48
children 813e5a52063d
rev   line source
adamc@26 1 (* Copyright (c) 2008, Adam Chlipala
adamc@26 2 * All rights reserved.
adamc@26 3 *
adamc@26 4 * Redistribution and use in source and binary forms, with or without
adamc@26 5 * modification, are permitted provided that the following conditions are met:
adamc@26 6 *
adamc@26 7 * - Redistributions of source code must retain the above copyright notice,
adamc@26 8 * this list of conditions and the following disclaimer.
adamc@26 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@26 10 * this list of conditions and the following disclaimer in the documentation
adamc@26 11 * and/or other materials provided with the distribution.
adamc@26 12 * - The names of contributors may not be used to endorse or promote products
adamc@26 13 * derived from this software without specific prior written permission.
adamc@26 14 *
adamc@26 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@26 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@26 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@26 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@26 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@26 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@26 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@26 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@26 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@26 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@26 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@26 26 *)
adamc@26 27
adamc@26 28 structure MonoUtil :> MONO_UTIL = struct
adamc@26 29
adamc@26 30 open Mono
adamc@26 31
adamc@26 32 structure S = Search
adamc@26 33
adamc@26 34 structure Typ = struct
adamc@26 35
adamc@26 36 fun mapfold fc =
adamc@26 37 let
adamc@26 38 fun mft c acc =
adamc@26 39 S.bindP (mft' c acc, fc)
adamc@26 40
adamc@26 41 and mft' (cAll as (c, loc)) =
adamc@26 42 case c of
adamc@26 43 TFun (t1, t2) =>
adamc@26 44 S.bind2 (mft t1,
adamc@26 45 fn t1' =>
adamc@26 46 S.map2 (mft t2,
adamc@26 47 fn t2' =>
adamc@26 48 (TFun (t1', t2'), loc)))
adamc@26 49 | TRecord xts =>
adamc@26 50 S.map2 (ListUtil.mapfold (fn (x, t) =>
adamc@26 51 S.map2 (mft t,
adamc@26 52 fn t' =>
adamc@26 53 (x, t')))
adamc@26 54 xts,
adamc@26 55 fn xts' => (TRecord xts', loc))
adamc@26 56 | TNamed _ => S.return2 cAll
adamc@51 57 | TFfi _ => S.return2 cAll
adamc@26 58 in
adamc@26 59 mft
adamc@26 60 end
adamc@26 61
adamc@26 62 fun map typ c =
adamc@26 63 case mapfold (fn c => fn () => S.Continue (typ c, ())) c () of
adamc@26 64 S.Return () => raise Fail "Mono_util.Typ.map"
adamc@26 65 | S.Continue (c, ()) => c
adamc@26 66
adamc@26 67 fun fold typ s c =
adamc@26 68 case mapfold (fn c => fn s => S.Continue (c, typ (c, s))) c s of
adamc@26 69 S.Continue (_, s) => s
adamc@26 70 | S.Return _ => raise Fail "MonoUtil.Typ.fold: Impossible"
adamc@26 71
adamc@26 72 fun exists typ k =
adamc@26 73 case mapfold (fn c => fn () =>
adamc@26 74 if typ c then
adamc@26 75 S.Return ()
adamc@26 76 else
adamc@26 77 S.Continue (c, ())) k () of
adamc@26 78 S.Return _ => true
adamc@26 79 | S.Continue _ => false
adamc@26 80
adamc@26 81 end
adamc@26 82
adamc@26 83 structure Exp = struct
adamc@26 84
adamc@26 85 datatype binder =
adamc@26 86 NamedT of string * int * typ option
adamc@26 87 | RelE of string * typ
adamc@26 88 | NamedE of string * int * typ * exp option
adamc@26 89
adamc@26 90 fun mapfoldB {typ = fc, exp = fe, bind} =
adamc@26 91 let
adamc@26 92 val mft = Typ.mapfold fc
adamc@26 93
adamc@26 94 fun mfe ctx e acc =
adamc@26 95 S.bindP (mfe' ctx e acc, fe ctx)
adamc@26 96
adamc@26 97 and mfe' ctx (eAll as (e, loc)) =
adamc@26 98 case e of
adamc@26 99 EPrim _ => S.return2 eAll
adamc@26 100 | ERel _ => S.return2 eAll
adamc@26 101 | ENamed _ => S.return2 eAll
adamc@51 102 | EFfi _ => S.return2 eAll
adamc@51 103 | EFfiApp (m, x, es) =>
adamc@51 104 S.map2 (ListUtil.mapfold (fn e => mfe ctx e) es,
adamc@51 105 fn es' =>
adamc@51 106 (EFfiApp (m, x, es'), loc))
adamc@26 107 | EApp (e1, e2) =>
adamc@26 108 S.bind2 (mfe ctx e1,
adamc@26 109 fn e1' =>
adamc@26 110 S.map2 (mfe ctx e2,
adamc@26 111 fn e2' =>
adamc@26 112 (EApp (e1', e2'), loc)))
adamc@26 113 | EAbs (x, dom, ran, e) =>
adamc@26 114 S.bind2 (mft dom,
adamc@26 115 fn dom' =>
adamc@26 116 S.bind2 (mft ran,
adamc@26 117 fn ran' =>
adamc@26 118 S.map2 (mfe (bind (ctx, RelE (x, dom'))) e,
adamc@26 119 fn e' =>
adamc@26 120 (EAbs (x, dom', ran', e'), loc))))
adamc@26 121
adamc@26 122 | ERecord xes =>
adamc@29 123 S.map2 (ListUtil.mapfold (fn (x, e, t) =>
adamc@29 124 S.bind2 (mfe ctx e,
adamc@26 125 fn e' =>
adamc@29 126 S.map2 (mft t,
adamc@29 127 fn t' =>
adamc@29 128 (x, e', t'))))
adamc@26 129 xes,
adamc@26 130 fn xes' =>
adamc@26 131 (ERecord xes', loc))
adamc@26 132 | EField (e, x) =>
adamc@26 133 S.map2 (mfe ctx e,
adamc@26 134 fn e' =>
adamc@26 135 (EField (e', x), loc))
adamc@94 136
adamc@94 137 | EStrcat (e1, e2) =>
adamc@94 138 S.bind2 (mfe ctx e1,
adamc@94 139 fn e1' =>
adamc@94 140 S.map2 (mfe ctx e2,
adamc@94 141 fn e2' =>
adamc@94 142 (EStrcat (e1', e2'), loc)))
adamc@102 143
adamc@102 144 | EWrite e =>
adamc@102 145 S.map2 (mfe ctx e,
adamc@102 146 fn e' =>
adamc@102 147 (EWrite e', loc))
adamc@106 148
adamc@106 149 | ESeq (e1, e2) =>
adamc@106 150 S.bind2 (mfe ctx e1,
adamc@106 151 fn e1' =>
adamc@106 152 S.map2 (mfe ctx e2,
adamc@106 153 fn e2' =>
adamc@106 154 (ESeq (e1', e2'), loc)))
adamc@26 155 in
adamc@26 156 mfe
adamc@26 157 end
adamc@26 158
adamc@26 159 fun mapfold {typ = fc, exp = fe} =
adamc@26 160 mapfoldB {typ = fc,
adamc@26 161 exp = fn () => fe,
adamc@26 162 bind = fn ((), _) => ()} ()
adamc@26 163
adamc@26 164 fun mapB {typ, exp, bind} ctx e =
adamc@26 165 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 166 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 167 bind = bind} ctx e () of
adamc@26 168 S.Continue (e, ()) => e
adamc@26 169 | S.Return _ => raise Fail "MonoUtil.Exp.mapB: Impossible"
adamc@26 170
adamc@26 171 fun map {typ, exp} e =
adamc@26 172 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 173 exp = fn e => fn () => S.Continue (exp e, ())} e () of
adamc@26 174 S.Return () => raise Fail "Mono_util.Exp.map"
adamc@26 175 | S.Continue (e, ()) => e
adamc@26 176
adamc@26 177 fun fold {typ, exp} s e =
adamc@26 178 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 179 exp = fn e => fn s => S.Continue (e, exp (e, s))} e s of
adamc@26 180 S.Continue (_, s) => s
adamc@26 181 | S.Return _ => raise Fail "MonoUtil.Exp.fold: Impossible"
adamc@26 182
adamc@26 183 fun exists {typ, exp} k =
adamc@26 184 case mapfold {typ = fn c => fn () =>
adamc@26 185 if typ c then
adamc@26 186 S.Return ()
adamc@26 187 else
adamc@26 188 S.Continue (c, ()),
adamc@26 189 exp = fn e => fn () =>
adamc@26 190 if exp e then
adamc@26 191 S.Return ()
adamc@26 192 else
adamc@26 193 S.Continue (e, ())} k () of
adamc@26 194 S.Return _ => true
adamc@26 195 | S.Continue _ => false
adamc@26 196
adamc@26 197 end
adamc@26 198
adamc@26 199 structure Decl = struct
adamc@26 200
adamc@26 201 datatype binder = datatype Exp.binder
adamc@26 202
adamc@26 203 fun mapfoldB {typ = fc, exp = fe, decl = fd, bind} =
adamc@26 204 let
adamc@26 205 val mft = Typ.mapfold fc
adamc@26 206
adamc@26 207 val mfe = Exp.mapfoldB {typ = fc, exp = fe, bind = bind}
adamc@26 208
adamc@26 209 fun mfd ctx d acc =
adamc@26 210 S.bindP (mfd' ctx d acc, fd ctx)
adamc@26 211
adamc@26 212 and mfd' ctx (dAll as (d, loc)) =
adamc@26 213 case d of
adamc@26 214 DVal (x, n, t, e) =>
adamc@26 215 S.bind2 (mft t,
adamc@26 216 fn t' =>
adamc@26 217 S.map2 (mfe ctx e,
adamc@26 218 fn e' =>
adamc@26 219 (DVal (x, n, t', e'), loc)))
adamc@100 220 | DPage (xts, e) =>
adamc@100 221 S.bind2 (ListUtil.mapfold (fn (x, t) =>
adamc@100 222 S.map2 (mft t,
adamc@100 223 fn t' =>
adamc@100 224 (x, t'))) xts,
adamc@100 225 fn xts' =>
adamc@100 226 S.map2 (mfe ctx e,
adamc@100 227 fn e' =>
adamc@100 228 (DPage (xts', e'), loc)))
adamc@26 229 in
adamc@26 230 mfd
adamc@26 231 end
adamc@26 232
adamc@26 233 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 234 mapfoldB {typ = fc,
adamc@26 235 exp = fn () => fe,
adamc@26 236 decl = fn () => fd,
adamc@26 237 bind = fn ((), _) => ()} ()
adamc@26 238
adamc@26 239 fun fold {typ, exp, decl} s d =
adamc@26 240 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 241 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 242 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 243 S.Continue (_, s) => s
adamc@26 244 | S.Return _ => raise Fail "MonoUtil.Decl.fold: Impossible"
adamc@26 245
adamc@26 246 end
adamc@26 247
adamc@26 248 structure File = struct
adamc@26 249
adamc@26 250 datatype binder = datatype Exp.binder
adamc@26 251
adamc@26 252 fun mapfoldB (all as {bind, ...}) =
adamc@26 253 let
adamc@26 254 val mfd = Decl.mapfoldB all
adamc@26 255
adamc@26 256 fun mff ctx ds =
adamc@26 257 case ds of
adamc@26 258 nil => S.return2 nil
adamc@26 259 | d :: ds' =>
adamc@26 260 S.bind2 (mfd ctx d,
adamc@26 261 fn d' =>
adamc@26 262 let
adamc@100 263 val ctx' =
adamc@26 264 case #1 d' of
adamc@100 265 DVal (x, n, t, e) => bind (ctx, NamedE (x, n, t, SOME e))
adamc@100 266 | DPage _ => ctx
adamc@26 267 in
adamc@26 268 S.map2 (mff ctx' ds',
adamc@26 269 fn ds' =>
adamc@26 270 d' :: ds')
adamc@26 271 end)
adamc@26 272 in
adamc@26 273 mff
adamc@26 274 end
adamc@26 275
adamc@26 276 fun mapfold {typ = fc, exp = fe, decl = fd} =
adamc@26 277 mapfoldB {typ = fc,
adamc@26 278 exp = fn () => fe,
adamc@26 279 decl = fn () => fd,
adamc@26 280 bind = fn ((), _) => ()} ()
adamc@26 281
adamc@26 282 fun mapB {typ, exp, decl, bind} ctx ds =
adamc@26 283 case mapfoldB {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@26 284 exp = fn ctx => fn e => fn () => S.Continue (exp ctx e, ()),
adamc@26 285 decl = fn ctx => fn d => fn () => S.Continue (decl ctx d, ()),
adamc@26 286 bind = bind} ctx ds () of
adamc@26 287 S.Continue (ds, ()) => ds
adamc@26 288 | S.Return _ => raise Fail "MonoUtil.File.mapB: Impossible"
adamc@26 289
adamc@96 290 fun map {typ, exp, decl} e =
adamc@96 291 case mapfold {typ = fn c => fn () => S.Continue (typ c, ()),
adamc@96 292 exp = fn e => fn () => S.Continue (exp e, ()),
adamc@96 293 decl = fn d => fn () => S.Continue (decl d, ())} e () of
adamc@96 294 S.Return () => raise Fail "Mono_util.File.map"
adamc@96 295 | S.Continue (e, ()) => e
adamc@96 296
adamc@26 297 fun fold {typ, exp, decl} s d =
adamc@26 298 case mapfold {typ = fn c => fn s => S.Continue (c, typ (c, s)),
adamc@26 299 exp = fn e => fn s => S.Continue (e, exp (e, s)),
adamc@26 300 decl = fn d => fn s => S.Continue (d, decl (d, s))} d s of
adamc@26 301 S.Continue (_, s) => s
adamc@26 302 | S.Return _ => raise Fail "MonoUtil.File.fold: Impossible"
adamc@26 303
adamc@26 304 end
adamc@26 305
adamc@26 306 end