annotate src/corify.sml @ 31:1c91c5e6840f

Simple signature matching
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Jun 2008 17:16:20 -0400
parents 537db4ee89f4
children 44b5405e74c7
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@16 53 | L.CApp (c1, c2) => (L'.CApp (corifyCon c1, corifyCon c2), loc)
adamc@16 54 | L.CAbs (x, k, c) => (L'.CAbs (x, corifyKind k, corifyCon c), loc)
adamc@16 55
adamc@16 56 | L.CName s => (L'.CName s, loc)
adamc@16 57
adamc@16 58 | L.CRecord (k, xcs) => (L'.CRecord (corifyKind k, map (fn (c1, c2) => (corifyCon c1, corifyCon c2)) xcs), loc)
adamc@16 59 | L.CConcat (c1, c2) => (L'.CConcat (corifyCon c1, corifyCon c2), loc)
adamc@16 60
adamc@16 61 | L.CError => raise Fail ("corifyCon: CError at " ^ EM.spanToString loc)
adamc@16 62 | L.CUnif (_, _, ref (SOME c)) => corifyCon c
adamc@16 63 | L.CUnif _ => raise Fail ("corifyCon: CUnif at " ^ EM.spanToString loc)
adamc@16 64
adamc@16 65 fun corifyExp (e, loc) =
adamc@16 66 case e of
adamc@16 67 L.EPrim p => (L'.EPrim p, loc)
adamc@16 68 | L.ERel n => (L'.ERel n, loc)
adamc@16 69 | L.ENamed n => (L'.ENamed n, loc)
adamc@16 70 | L.EApp (e1, e2) => (L'.EApp (corifyExp e1, corifyExp e2), loc)
adamc@26 71 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, corifyCon dom, corifyCon ran, corifyExp e1), loc)
adamc@16 72 | L.ECApp (e1, c) => (L'.ECApp (corifyExp e1, corifyCon c), loc)
adamc@16 73 | L.ECAbs (_, x, k, e1) => (L'.ECAbs (x, corifyKind k, corifyExp e1), loc)
adamc@16 74
adamc@29 75 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (corifyCon c, corifyExp e, corifyCon t)) xes), loc)
adamc@16 76 | L.EField (e1, c, {field, rest}) => (L'.EField (corifyExp e1, corifyCon c,
adamc@16 77 {field = corifyCon field, rest = corifyCon rest}), loc)
adamc@16 78
adamc@16 79 | L.EError => raise Fail ("corifyExp: EError at " ^ EM.spanToString loc)
adamc@16 80
adamc@16 81 fun corifyDecl (d, loc : EM.span) =
adamc@16 82 case d of
adamc@16 83 L.DCon (x, n, k, c) => (L'.DCon (x, n, corifyKind k, corifyCon c), loc)
adamc@16 84 | L.DVal (x, n, t, e) => (L'.DVal (x, n, corifyCon t, corifyExp e), loc)
adamc@16 85
adamc@31 86 | L.DSgn _ => raise Fail "Not ready to corify signature"
adamc@31 87 | L.DStr _ => raise Fail "Not ready to corify structure"
adamc@31 88
adamc@16 89 val corify = map corifyDecl
adamc@16 90
adamc@16 91 end