annotate src/corify.sml @ 34:44b5405e74c7

Elaborating module projection
author Adam Chlipala <adamc@hcoop.net>
date Tue, 17 Jun 2008 16:38:54 -0400
parents 1c91c5e6840f
children 02f42e9a1825
rev   line source
adamc@16 1 (* Copyright (c) 2008, Adam Chlipala
adamc@16 2 * All rights reserved.
adamc@16 3 *
adamc@16 4 * Redistribution and use in source and binary forms, with or without
adamc@16 5 * modification, are permitted provided that the following conditions are met:
adamc@16 6 *
adamc@16 7 * - Redistributions of source code must retain the above copyright notice,
adamc@16 8 * this list of conditions and the following disclaimer.
adamc@16 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@16 10 * this list of conditions and the following disclaimer in the documentation
adamc@16 11 * and/or other materials provided with the distribution.
adamc@16 12 * - The names of contributors may not be used to endorse or promote products
adamc@16 13 * derived from this software without specific prior written permission.
adamc@16 14 *
adamc@16 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@16 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@16 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@16 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@16 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@16 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@16 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@16 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@16 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@16 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@16 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@16 26 *)
adamc@16 27
adamc@16 28 structure Corify :> CORIFY = struct
adamc@16 29
adamc@16 30 structure EM = ErrorMsg
adamc@16 31 structure L = Elab
adamc@16 32 structure L' = Core
adamc@16 33
adamc@16 34 fun corifyKind (k, loc) =
adamc@16 35 case k of
adamc@16 36 L.KType => (L'.KType, loc)
adamc@16 37 | L.KArrow (k1, k2) => (L'.KArrow (corifyKind k1, corifyKind k2), loc)
adamc@16 38 | L.KName => (L'.KName, loc)
adamc@16 39 | L.KRecord k => (L'.KRecord (corifyKind k), loc)
adamc@16 40
adamc@16 41 | L.KError => raise Fail ("corifyKind: KError at " ^ EM.spanToString loc)
adamc@16 42 | L.KUnif (_, ref (SOME k)) => corifyKind k
adamc@16 43 | L.KUnif _ => raise Fail ("corifyKind: KUnif at " ^ EM.spanToString loc)
adamc@16 44
adamc@16 45 fun corifyCon (c, loc) =
adamc@16 46 case c of
adamc@16 47 L.TFun (t1, t2) => (L'.TFun (corifyCon t1, corifyCon t2), loc)
adamc@16 48 | L.TCFun (_, x, k, t) => (L'.TCFun (x, corifyKind k, corifyCon t), loc)
adamc@16 49 | L.TRecord c => (L'.TRecord (corifyCon c), loc)
adamc@16 50
adamc@16 51 | L.CRel n => (L'.CRel n, loc)
adamc@16 52 | L.CNamed n => (L'.CNamed n, loc)
adamc@34 53 | L.CModProj _ => raise Fail "Corify CModProj"
adamc@34 54
adamc@16 55 | L.CApp (c1, c2) => (L'.CApp (corifyCon c1, corifyCon c2), loc)
adamc@16 56 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon c), loc)
adamc@16 57
adamc@16 58 | L.CName s => (L'.CName s, loc)
adamc@16 59
adamc@16 60 | L.CRecord (k, xcs) => (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon c1, corifyCon c2)) xcs), loc)
adamc@16 61 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon c1, corifyCon c2), loc)
adamc@16 62
adamc@16 63 | L.CError => raise Fail ("corifyCon: CError at " ^ EM.spanToString loc)
adamc@16 64 | L.CUnif (_, _, ref (SOME c)) => corifyCon c
adamc@16 65 | L.CUnif _ => raise Fail ("corifyCon: CUnif at " ^ EM.spanToString loc)
adamc@16 66
adamc@16 67 fun corifyExp (e, loc) =
adamc@16 68 case e of
adamc@16 69 L.EPrim p => (L'.EPrim p, loc)
adamc@16 70 | L.ERel n => (L'.ERel n, loc)
adamc@16 71 | L.ENamed n => (L'.ENamed n, loc)
adamc@34 72 | L.EModProj _ => raise Fail "Corify EModProj"
adamc@16 73 | L.EApp (e1, e2) => (L'.EApp (corifyExp e1, corifyExp e2), loc)
adamc@26 74 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon dom, corifyCon ran, corifyExp e1), loc)
adamc@16 75 | L.ECApp (e1, c) => (L'.ECApp (corifyExp e1, corifyCon c), loc)
adamc@16 76 | L.ECAbs (_, x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp e1), loc)
adamc@16 77
adamc@29 78 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon c, corifyExp e, corifyCon t)) xes), loc)
adamc@16 79 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp e1, corifyCon c,
adamc@16 80 {field = corifyCon field, rest = corifyCon rest}), loc)
adamc@16 81
adamc@16 82 | L.EError => raise Fail ("corifyExp: EError at " ^ EM.spanToString loc)
adamc@16 83
adamc@16 84 fun corifyDecl (d, loc : EM.span) =
adamc@16 85 case d of
adamc@16 86 L.DCon (x, n, k, c) => (L'.DCon (x, n, corifyKind k, corifyCon c), loc)
adamc@16 87 | L.DVal (x, n, t, e) => (L'.DVal (x, n, corifyCon t, corifyExp e), loc)
adamc@16 88
adamc@31 89 | L.DSgn _ => raise Fail "Not ready to corify signature"
adamc@31 90 | L.DStr _ => raise Fail "Not ready to corify structure"
adamc@31 91
adamc@16 92 val corify = map corifyDecl
adamc@16 93
adamc@16 94 end