annotate src/elab_util.sml @ 14:f1c36df29ed7

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