annotate src/elab_ops.sml @ 447:b77863cd0be2

Elaborating 'let'
author Adam Chlipala <adamc@hcoop.net>
date Sat, 01 Nov 2008 11:17:29 -0400
parents b85e6ba56618
children 1bbcc3345d12
rev   line source
adamc@81 1 (* Copyright (c) 2008, Adam Chlipala
adamc@81 2 * All rights reserved.
adamc@81 3 *
adamc@81 4 * Redistribution and use in source and binary forms, with or without
adamc@81 5 * modification, are permitted provided that the following conditions are met:
adamc@81 6 *
adamc@81 7 * - Redistributions of source code must retain the above copyright notice,
adamc@81 8 * this list of conditions and the following disclaimer.
adamc@81 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@81 10 * this list of conditions and the following disclaimer in the documentation
adamc@81 11 * and/or other materials provided with the distribution.
adamc@81 12 * - The names of contributors may not be used to endorse or promote products
adamc@81 13 * derived from this software without specific prior written permission.
adamc@81 14 *
adamc@81 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@81 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@81 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@81 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@81 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@81 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@81 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@81 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@81 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@81 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@81 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@81 26 *)
adamc@81 27
adamc@81 28 structure ElabOps :> ELAB_OPS = struct
adamc@81 29
adamc@81 30 open Elab
adamc@81 31
adamc@81 32 structure E = ElabEnv
adamc@81 33 structure U = ElabUtil
adamc@81 34
adamc@81 35 val liftConInCon = E.liftConInCon
adamc@81 36
adamc@81 37 val subConInCon =
adamc@81 38 U.Con.mapB {kind = fn k => k,
adamc@81 39 con = fn (xn, rep) => fn c =>
adamc@81 40 case c of
adamc@81 41 CRel xn' =>
adamc@81 42 (case Int.compare (xn', xn) of
adamc@81 43 EQUAL => #1 rep
adamc@81 44 | GREATER => CRel (xn' - 1)
adamc@81 45 | LESS => c)
adamc@81 46 (*| CUnif _ => raise SynUnif*)
adamc@81 47 | _ => c,
adamc@81 48 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
adamc@81 49 | (ctx, _) => ctx}
adamc@81 50
adamc@81 51 fun subStrInSgn (m1, m2) =
adamc@81 52 U.Sgn.map {kind = fn k => k,
adamc@81 53 con = fn c as CModProj (m1', ms, x) =>
adamc@81 54 if m1 = m1' then
adamc@81 55 CModProj (m2, ms, x)
adamc@81 56 else
adamc@81 57 c
adamc@81 58 | c => c,
adamc@81 59 sgn_item = fn sgi => sgi,
adamc@81 60 sgn = fn sgn => sgn}
adamc@81 61
adamc@81 62
adamc@81 63 fun hnormCon env (cAll as (c, loc)) =
adamc@81 64 case c of
adamc@81 65 CUnif (_, _, _, ref (SOME c)) => hnormCon env c
adamc@81 66
adamc@81 67 | CNamed xn =>
adamc@81 68 (case E.lookupCNamed env xn of
adamc@81 69 (_, _, SOME c') => hnormCon env c'
adamc@81 70 | _ => cAll)
adamc@81 71
adamc@81 72 | CModProj (n, ms, x) =>
adamc@81 73 let
adamc@81 74 val (_, sgn) = E.lookupStrNamed env n
adamc@81 75 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
adamc@81 76 case E.projectStr env {sgn = sgn, str = str, field = m} of
adamc@81 77 NONE => raise Fail "hnormCon: Unknown substructure"
adamc@81 78 | SOME sgn => ((StrProj (str, m), loc), sgn))
adamc@81 79 ((StrVar n, loc), sgn) ms
adamc@81 80 in
adamc@81 81 case E.projectCon env {sgn = sgn, str = str, field = x} of
adamc@81 82 NONE => raise Fail "kindof: Unknown con in structure"
adamc@81 83 | SOME (_, NONE) => cAll
adamc@81 84 | SOME (_, SOME c) => hnormCon env c
adamc@81 85 end
adamc@81 86
adamc@81 87 | CApp (c1, c2) =>
adamc@81 88 (case #1 (hnormCon env c1) of
adamc@81 89 CAbs (x, k, cb) =>
adamc@81 90 let
adamc@81 91 val sc = (hnormCon env (subConInCon (0, c2) cb))
adamc@81 92 handle SynUnif => cAll
adamc@81 93 (*val env' = E.pushCRel env x k*)
adamc@81 94 in
adamc@328 95 (*Print.eprefaces "Subst" [("x", Print.PD.string x),
adamc@328 96 ("cb", ElabPrint.p_con env' cb),
adamc@328 97 ("c2", ElabPrint.p_con env c2),
adamc@328 98 ("sc", ElabPrint.p_con env sc)];*)
adamc@81 99 sc
adamc@81 100 end
adamc@326 101 | c1' as CApp (c', i) =>
adamc@326 102 let
adamc@326 103 fun default () = (CApp ((c1', loc), hnormCon env c2), loc)
adamc@326 104 in
adamc@326 105 case #1 (hnormCon env c') of
adamc@326 106 CApp (c', f) =>
adamc@326 107 (case #1 (hnormCon env c') of
adamc@326 108 CFold ks =>
adamc@326 109 (case #1 (hnormCon env c2) of
adamc@326 110 CRecord (_, []) => hnormCon env i
adamc@326 111 | CRecord (k, (x, c) :: rest) =>
adamc@326 112 hnormCon env
adamc@326 113 (CApp ((CApp ((CApp (f, x), loc), c), loc),
adamc@81 114 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
adamc@326 115 (CRecord (k, rest), loc)), loc)), loc)
adamc@326 116 | CConcat ((CRecord (k, (x, c) :: rest), _), rest') =>
adamc@326 117 let
adamc@326 118 val rest'' = (CConcat ((CRecord (k, rest), loc), rest'), loc)
adamc@81 119
adamc@326 120 (*val ccc = (CApp ((CApp ((CApp (f, x), loc), c), loc),
adamc@326 121 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
adamc@326 122 rest''), loc)), loc)*)
adamc@326 123 in
adamc@326 124 (*eprefaces "Red to" [("ccc", p_con env ccc), ("ccc'", p_con env (hnormCon env ccc))];*)
adamc@326 125 hnormCon env
adamc@326 126 (CApp ((CApp ((CApp (f, x), loc), c), loc),
adamc@81 127 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
adamc@326 128 rest''), loc)), loc)
adamc@326 129 end
adamc@329 130 | _ =>
adamc@329 131 let
adamc@329 132 fun cunif () =
adamc@329 133 let
adamc@329 134 val r = ref NONE
adamc@329 135 in
adamc@329 136 (r, (CUnif (loc, (KType, loc), "_", r), loc))
adamc@329 137 end
adamc@329 138
adamc@329 139 val (nmR, nm) = cunif ()
adamc@329 140 val (vR, v) = cunif ()
adamc@329 141 val (rR, r) = cunif ()
adamc@329 142
adamc@329 143 val c = f
adamc@329 144 val c = (CApp (c, nm), loc)
adamc@329 145 val c = (CApp (c, v), loc)
adamc@329 146 val c = (CApp (c, r), loc)
adamc@329 147 fun unconstraint c =
adamc@329 148 case hnormCon env c of
adamc@345 149 (CDisjoint (_, _, _, c), _) => unconstraint c
adamc@329 150 | c => c
adamc@329 151 val c = unconstraint c
adamc@339 152
adamc@339 153 fun tryFusion () =
adamc@339 154 let
adamc@339 155 fun fuse (dom, new_v, r') =
adamc@339 156 let
adamc@339 157 val ran = #2 ks
adamc@339 158
adamc@339 159 val f = (CApp (f, (CRel 2, loc)), loc)
adamc@339 160 val f = (CApp (f, new_v), loc)
adamc@339 161 val f = (CApp (f, (CRel 0, loc)), loc)
adamc@339 162 val f = (CAbs ("acc", ran, f), loc)
adamc@339 163 val f = (CAbs ("v", dom, f), loc)
adamc@339 164 val f = (CAbs ("nm", (KName, loc), f), loc)
adamc@339 165
adamc@339 166 val c = (CFold (dom, ran), loc)
adamc@339 167 val c = (CApp (c, f), loc)
adamc@339 168 val c = (CApp (c, i), loc)
adamc@339 169 val c = (CApp (c, r'), loc)
adamc@339 170 in
adamc@339 171 hnormCon env c
adamc@339 172 end
adamc@339 173 in
adamc@339 174 case #1 (hnormCon env c2) of
adamc@339 175 CApp (f, r') =>
adamc@339 176 (case #1 (hnormCon env f) of
adamc@339 177 CApp (f, inner_i) =>
adamc@339 178 (case (#1 (hnormCon env f), #1 (hnormCon env inner_i)) of
adamc@339 179 (CApp (f, inner_f), CRecord (_, [])) =>
adamc@339 180 (case #1 (hnormCon env f) of
adamc@339 181 CFold (dom, _) =>
adamc@339 182 let
adamc@339 183 val c = inner_f
adamc@339 184 val c = (CApp (c, nm), loc)
adamc@339 185 val c = (CApp (c, v), loc)
adamc@339 186 val c = (CApp (c, r), loc)
adamc@339 187 val c = unconstraint c
adamc@339 188
adamc@339 189 (*val () = Print.prefaces "Onto something!"
adamc@339 190 [("c", ElabPrint.p_con env cAll),
adamc@339 191 ("c'", ElabPrint.p_con env c)]*)
adamc@339 192
adamc@339 193 in
adamc@339 194 case #1 (hnormCon env c) of
adamc@339 195 CConcat (first, rest) =>
adamc@339 196 (case (#1 (hnormCon env first),
adamc@339 197 #1 (hnormCon env rest)) of
adamc@339 198 (CRecord (_, [(nm', v')]),
adamc@339 199 CUnif (_, _, _, rR')) =>
adamc@339 200 (case #1 (hnormCon env nm') of
adamc@339 201 CUnif (_, _, _, nmR') =>
adamc@339 202 if rR' = rR andalso nmR' = nmR then
adamc@339 203 (nmR := SOME (CRel 2, loc);
adamc@339 204 vR := SOME (CRel 1, loc);
adamc@339 205 rR := SOME (CError, loc);
adamc@339 206 fuse (dom, v', r'))
adamc@339 207 else
adamc@339 208 default ()
adamc@339 209 | _ => default ())
adamc@339 210 | _ => default ())
adamc@339 211 | _ => default ()
adamc@339 212 end
adamc@339 213 | _ => default ())
adamc@339 214 | _ => default ())
adamc@339 215 | _ => default ())
adamc@339 216 | _ => default ()
adamc@339 217 end
adamc@329 218 in
adamc@329 219 (*Print.prefaces "Consider" [("c", ElabPrint.p_con env c)];*)
adamc@329 220 case (hnormCon env i, unconstraint c) of
adamc@329 221 ((CRecord (_, []), _),
adamc@329 222 (CConcat (c1, c2'), _)) =>
adamc@329 223 (case (hnormCon env c1, hnormCon env c2') of
adamc@329 224 ((CRecord (_, [(nm', v')]), _),
adamc@329 225 (CUnif (_, _, _, rR'), _)) =>
adamc@329 226 (case (hnormCon env nm', hnormCon env v') of
adamc@329 227 ((CUnif (_, _, _, nmR'), _),
adamc@329 228 (CUnif (_, _, _, vR'), _)) =>
adamc@329 229 if nmR' = nmR andalso vR' = vR andalso rR' = rR then
adamc@329 230 hnormCon env c2
adamc@329 231 else
adamc@339 232 tryFusion ()
adamc@339 233 | _ => tryFusion ())
adamc@339 234 | _ => tryFusion ())
adamc@339 235 | _ => tryFusion ()
adamc@329 236 end)
adamc@326 237 | _ => default ())
adamc@326 238 | _ => default ()
adamc@326 239 end
adamc@326 240 | c1' => (CApp ((c1', loc), hnormCon env c2), loc))
adamc@81 241
adamc@81 242 | CConcat (c1, c2) =>
adamc@81 243 (case (hnormCon env c1, hnormCon env c2) of
adamc@81 244 ((CRecord (k, xcs1), loc), (CRecord (_, xcs2), _)) =>
adamc@81 245 (CRecord (k, xcs1 @ xcs2), loc)
adamc@81 246 | ((CRecord (_, []), _), c2') => c2'
adamc@81 247 | ((CConcat (c11, c12), loc), c2') =>
adamc@81 248 hnormCon env (CConcat (c11, (CConcat (c12, c2'), loc)), loc)
adamc@209 249 | (c1', (CRecord (_, []), _)) => c1'
adamc@329 250 | (c1', c2') => (CConcat (c1', c2'), loc))
adamc@81 251
adamc@207 252 | CProj (c, n) =>
adamc@207 253 (case hnormCon env c of
adamc@207 254 (CTuple cs, _) => hnormCon env (List.nth (cs, n - 1))
adamc@207 255 | _ => cAll)
adamc@207 256
adamc@81 257 | _ => cAll
adamc@81 258
adamc@81 259 end