annotate src/explify.sml @ 1298:e665527fce1c

Support empty datatypes, which can now be generated via 'variant []'
author Adam Chlipala <adam@chlipala.net>
date Thu, 09 Sep 2010 12:42:25 -0400
parents b4480a56cab7
children d008c4c43a0a
rev   line source
adam@1294 1 (* Copyright (c) 2008-2010, Adam Chlipala
adamc@38 2 * All rights reserved.
adamc@38 3 *
adamc@38 4 * Redistribution and use in source and binary forms, with or without
adamc@38 5 * modification, are permitted provided that the following conditions are met:
adamc@38 6 *
adamc@38 7 * - Redistributions of source code must retain the above copyright notice,
adamc@38 8 * this list of conditions and the following disclaimer.
adamc@38 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@38 10 * this list of conditions and the following disclaimer in the documentation
adamc@38 11 * and/or other materials provided with the distribution.
adamc@38 12 * - The names of contributors may not be used to endorse or promote products
adamc@38 13 * derived from this software without specific prior written permission.
adamc@38 14 *
adamc@38 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@38 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@38 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@38 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@38 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@38 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@38 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@38 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@38 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@38 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@38 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@38 26 *)
adamc@38 27
adamc@38 28 structure Explify :> EXPLIFY = struct
adamc@38 29
adamc@38 30 structure EM = ErrorMsg
adamc@38 31 structure L = Elab
adamc@38 32 structure L' = Expl
adamc@38 33
adamc@38 34 fun explifyKind (k, loc) =
adamc@38 35 case k of
adamc@38 36 L.KType => (L'.KType, loc)
adamc@38 37 | L.KArrow (k1, k2) => (L'.KArrow (explifyKind k1, explifyKind k2), loc)
adamc@38 38 | L.KName => (L'.KName, loc)
adamc@38 39 | L.KRecord k => (L'.KRecord (explifyKind k), loc)
adamc@38 40
adamc@87 41 | L.KUnit => (L'.KUnit, loc)
adamc@213 42 | L.KTuple ks => (L'.KTuple (map explifyKind ks), loc)
adamc@82 43
adamc@38 44 | L.KError => raise Fail ("explifyKind: KError at " ^ EM.spanToString loc)
adamc@76 45 | L.KUnif (_, _, ref (SOME k)) => explifyKind k
adamc@38 46 | L.KUnif _ => raise Fail ("explifyKind: KUnif at " ^ EM.spanToString loc)
adamc@38 47
adamc@624 48 | L.KRel n => (L'.KRel n, loc)
adamc@624 49 | L.KFun (x, k) => (L'.KFun (x, explifyKind k), loc)
adamc@624 50
adamc@38 51 fun explifyCon (c, loc) =
adamc@38 52 case c of
adamc@38 53 L.TFun (t1, t2) => (L'.TFun (explifyCon t1, explifyCon t2), loc)
adamc@38 54 | L.TCFun (_, x, k, t) => (L'.TCFun (x, explifyKind k, explifyCon t), loc)
adamc@628 55 | L.TDisjoint (_, _, t) => explifyCon t
adamc@38 56 | L.TRecord c => (L'.TRecord (explifyCon c), loc)
adamc@38 57
adamc@38 58 | L.CRel n => (L'.CRel n, loc)
adamc@38 59 | L.CNamed n => (L'.CNamed n, loc)
adamc@38 60 | L.CModProj (m, ms, x) => (L'.CModProj (m, ms, x), loc)
adamc@38 61
adamc@38 62 | L.CApp (c1, c2) => (L'.CApp (explifyCon c1, explifyCon c2), loc)
adamc@38 63 | L.CAbs (x, k, c) => (L'.CAbs (x, explifyKind k, explifyCon c), loc)
adamc@38 64
adamc@38 65 | L.CName s => (L'.CName s, loc)
adamc@38 66
adamc@38 67 | L.CRecord (k, xcs) => (L'.CRecord (explifyKind k, map (fn (c1, c2) => (explifyCon c1, explifyCon c2)) xcs), loc)
adamc@38 68 | L.CConcat (c1, c2) => (L'.CConcat (explifyCon c1, explifyCon c2), loc)
adamc@621 69 | L.CMap (dom, ran) => (L'.CMap (explifyKind dom, explifyKind ran), loc)
adamc@38 70
adamc@87 71 | L.CUnit => (L'.CUnit, loc)
adamc@82 72
adamc@213 73 | L.CTuple cs => (L'.CTuple (map explifyCon cs), loc)
adamc@213 74 | L.CProj (c, n) => (L'.CProj (explifyCon c, n), loc)
adamc@208 75
adamc@38 76 | L.CError => raise Fail ("explifyCon: CError at " ^ EM.spanToString loc)
adamc@76 77 | L.CUnif (_, _, _, ref (SOME c)) => explifyCon c
adamc@38 78 | L.CUnif _ => raise Fail ("explifyCon: CUnif at " ^ EM.spanToString loc)
adamc@38 79
adamc@624 80 | L.CKAbs (x, c) => (L'.CKAbs (x, explifyCon c), loc)
adamc@624 81 | L.CKApp (c, k) => (L'.CKApp (explifyCon c, explifyKind k), loc)
adamc@624 82 | L.TKFun (x, c) => (L'.TKFun (x, explifyCon c), loc)
adamc@624 83
adamc@176 84 fun explifyPatCon pc =
adamc@176 85 case pc of
adamc@176 86 L.PConVar n => L'.PConVar n
adamc@176 87 | L.PConProj x => L'.PConProj x
adamc@176 88
adamc@176 89 fun explifyPat (p, loc) =
adamc@176 90 case p of
adamc@176 91 L.PWild => (L'.PWild, loc)
adamc@182 92 | L.PVar (x, t) => (L'.PVar (x, explifyCon t), loc)
adamc@176 93 | L.PPrim p => (L'.PPrim p, loc)
adamc@191 94 | L.PCon (dk, pc, cs, po) => (L'.PCon (dk, explifyPatCon pc, map explifyCon cs, Option.map explifyPat po), loc)
adamc@182 95 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, explifyPat p, explifyCon t)) xps), loc)
adamc@176 96
adamc@38 97 fun explifyExp (e, loc) =
adamc@38 98 case e of
adamc@38 99 L.EPrim p => (L'.EPrim p, loc)
adamc@38 100 | L.ERel n => (L'.ERel n, loc)
adamc@38 101 | L.ENamed n => (L'.ENamed n, loc)
adamc@38 102 | L.EModProj (m, ms, x) => (L'.EModProj (m, ms, x), loc)
adamc@38 103 | L.EApp (e1, e2) => (L'.EApp (explifyExp e1, explifyExp e2), loc)
adamc@38 104 | L.EAbs (x, dom, ran, e1) => (L'.EAbs (x, explifyCon dom, explifyCon ran, explifyExp e1), loc)
adamc@38 105 | L.ECApp (e1, c) => (L'.ECApp (explifyExp e1, explifyCon c), loc)
adamc@38 106 | L.ECAbs (_, x, k, e1) => (L'.ECAbs (x, explifyKind k, explifyExp e1), loc)
adamc@38 107
adamc@38 108 | L.ERecord xes => (L'.ERecord (map (fn (c, e, t) => (explifyCon c, explifyExp e, explifyCon t)) xes), loc)
adamc@38 109 | L.EField (e1, c, {field, rest}) => (L'.EField (explifyExp e1, explifyCon c,
adamc@38 110 {field = explifyCon field, rest = explifyCon rest}), loc)
adamc@445 111 | L.EConcat (e1, c1, e2, c2) => (L'.EConcat (explifyExp e1, explifyCon c1, explifyExp e2, explifyCon c2),
adamc@445 112 loc)
adamc@149 113 | L.ECut (e1, c, {field, rest}) => (L'.ECut (explifyExp e1, explifyCon c,
adamc@149 114 {field = explifyCon field, rest = explifyCon rest}), loc)
adamc@493 115 | L.ECutMulti (e1, c, {rest}) => (L'.ECutMulti (explifyExp e1, explifyCon c,
adamc@493 116 {rest = explifyCon rest}), loc)
adamc@182 117 | L.ECase (e, pes, {disc, result}) =>
adamc@182 118 (L'.ECase (explifyExp e,
adamc@182 119 map (fn (p, e) => (explifyPat p, explifyExp e)) pes,
adamc@182 120 {disc = explifyCon disc, result = explifyCon result}), loc)
adamc@171 121
adamc@38 122 | L.EError => raise Fail ("explifyExp: EError at " ^ EM.spanToString loc)
adamc@228 123 | L.EUnif (ref (SOME e)) => explifyExp e
adamc@228 124 | L.EUnif _ => raise Fail ("explifyExp: Undetermined EUnif at " ^ EM.spanToString loc)
adamc@38 125
adamc@825 126 | L.ELet (des, e, t) =>
adamc@449 127 foldr (fn ((de, loc), e) =>
adamc@449 128 case de of
adamc@449 129 L.EDValRec _ => raise Fail "explifyExp: Local 'val rec' remains"
adamc@825 130 | L.EDVal ((L.PVar (x, _), _), t', e') => (L'.ELet (x, explifyCon t', explifyExp e', e), loc)
adamc@825 131 | L.EDVal (p, t', e') => (L'.ECase (explifyExp e',
adamc@825 132 [(explifyPat p, e)],
adamc@825 133 {disc = explifyCon t', result = explifyCon t}), loc))
adamc@449 134 (explifyExp e) des
adamc@449 135
adamc@624 136 | L.EKAbs (x, e) => (L'.EKAbs (x, explifyExp e), loc)
adamc@624 137 | L.EKApp (e, k) => (L'.EKApp (explifyExp e, explifyKind k), loc)
adamc@624 138
adamc@38 139 fun explifySgi (sgi, loc) =
adamc@38 140 case sgi of
adamc@88 141 L.SgiConAbs (x, n, k) => SOME (L'.SgiConAbs (x, n, explifyKind k), loc)
adamc@88 142 | L.SgiCon (x, n, k, c) => SOME (L'.SgiCon (x, n, explifyKind k, explifyCon c), loc)
adamc@806 143 | L.SgiDatatype dts => SOME (L'.SgiDatatype (map (fn (x, n, xs, xncs) =>
adamc@806 144 (x, n, xs,
adamc@806 145 map (fn (x, n, co) =>
adamc@806 146 (x, n, Option.map explifyCon co)) xncs)) dts), loc)
adamc@191 147 | L.SgiDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@191 148 SOME (L'.SgiDatatypeImp (x, n, m1, ms, s, xs, map (fn (x, n, co) =>
adamc@191 149 (x, n, Option.map explifyCon co)) xncs), loc)
adamc@88 150 | L.SgiVal (x, n, c) => SOME (L'.SgiVal (x, n, explifyCon c), loc)
adamc@88 151 | L.SgiStr (x, n, sgn) => SOME (L'.SgiStr (x, n, explifySgn sgn), loc)
adamc@88 152 | L.SgiSgn (x, n, sgn) => SOME (L'.SgiSgn (x, n, explifySgn sgn), loc)
adamc@88 153 | L.SgiConstraint _ => NONE
adamc@563 154 | L.SgiClassAbs (x, n, k) => SOME (L'.SgiConAbs (x, n, (L'.KArrow (explifyKind k, (L'.KType, loc)), loc)), loc)
adamc@563 155 | L.SgiClass (x, n, k, c) => SOME (L'.SgiCon (x, n, (L'.KArrow (explifyKind k, (L'.KType, loc)), loc),
adamc@563 156 explifyCon c), loc)
adamc@38 157
adamc@38 158 and explifySgn (sgn, loc) =
adamc@38 159 case sgn of
adamc@88 160 L.SgnConst sgis => (L'.SgnConst (List.mapPartial explifySgi sgis), loc)
adamc@38 161 | L.SgnVar n => (L'.SgnVar n, loc)
adamc@45 162 | L.SgnFun (m, n, dom, ran) => (L'.SgnFun (m, n, explifySgn dom, explifySgn ran), loc)
adamc@45 163 | L.SgnWhere (sgn, x, c) => (L'.SgnWhere (explifySgn sgn, x, explifyCon c), loc)
adamc@64 164 | L.SgnProj x => (L'.SgnProj x, loc)
adamc@38 165 | L.SgnError => raise Fail ("explifySgn: SgnError at " ^ EM.spanToString loc)
adamc@38 166
adamc@38 167 fun explifyDecl (d, loc : EM.span) =
adamc@38 168 case d of
adamc@88 169 L.DCon (x, n, k, c) => SOME (L'.DCon (x, n, explifyKind k, explifyCon c), loc)
adamc@806 170 | L.DDatatype dts => SOME (L'.DDatatype (map (fn (x, n, xs, xncs) =>
adamc@806 171 (x, n, xs,
adamc@806 172 map (fn (x, n, co) =>
adamc@806 173 (x, n, Option.map explifyCon co)) xncs)) dts), loc)
adamc@191 174 | L.DDatatypeImp (x, n, m1, ms, s, xs, xncs) =>
adamc@191 175 SOME (L'.DDatatypeImp (x, n, m1, ms, s, xs,
adamc@191 176 map (fn (x, n, co) =>
adamc@191 177 (x, n, Option.map explifyCon co)) xncs), loc)
adamc@88 178 | L.DVal (x, n, t, e) => SOME (L'.DVal (x, n, explifyCon t, explifyExp e), loc)
adamc@124 179 | L.DValRec vis => SOME (L'.DValRec (map (fn (x, n, t, e) => (x, n, explifyCon t, explifyExp e)) vis), loc)
adamc@38 180
adamc@88 181 | L.DSgn (x, n, sgn) => SOME (L'.DSgn (x, n, explifySgn sgn), loc)
adamc@88 182 | L.DStr (x, n, sgn, str) => SOME (L'.DStr (x, n, explifySgn sgn, explifyStr str), loc)
adamc@88 183 | L.DFfiStr (x, n, sgn) => SOME (L'.DFfiStr (x, n, explifySgn sgn), loc)
adamc@88 184 | L.DConstraint (c1, c2) => NONE
adamc@109 185 | L.DExport (en, sgn, str) => SOME (L'.DExport (en, explifySgn sgn, explifyStr str), loc)
adamc@707 186 | L.DTable (nt, x, n, c, pe, pc, ce, cc) =>
adamc@707 187 SOME (L'.DTable (nt, x, n, explifyCon c,
adamc@707 188 explifyExp pe, explifyCon pc,
adamc@707 189 explifyExp ce, explifyCon cc), loc)
adamc@754 190 | L.DView (nt, x, n, e, c) =>
adamc@754 191 SOME (L'.DView (nt, x, n, explifyExp e, explifyCon c), loc)
adamc@338 192 | L.DSequence (nt, x, n) => SOME (L'.DSequence (nt, x, n), loc)
adamc@563 193 | L.DClass (x, n, k, c) => SOME (L'.DCon (x, n,
adamc@563 194 (L'.KArrow (explifyKind k, (L'.KType, loc)), loc), explifyCon c), loc)
adamc@271 195 | L.DDatabase s => SOME (L'.DDatabase s, loc)
adamc@460 196 | L.DCookie (nt, x, n, c) => SOME (L'.DCookie (nt, x, n, explifyCon c), loc)
adamc@720 197 | L.DStyle (nt, x, n) => SOME (L'.DStyle (nt, x, n), loc)
adamc@1075 198 | L.DTask (e1, e2) => SOME (L'.DTask (explifyExp e1, explifyExp e2), loc)
adamc@1199 199 | L.DPolicy e1 => SOME (L'.DPolicy (explifyExp e1), loc)
adam@1294 200 | L.DOnError v => SOME (L'.DOnError v, loc)
adamc@38 201
adamc@38 202 and explifyStr (str, loc) =
adamc@38 203 case str of
adamc@88 204 L.StrConst ds => (L'.StrConst (List.mapPartial explifyDecl ds), loc)
adamc@38 205 | L.StrVar n => (L'.StrVar n, loc)
adamc@38 206 | L.StrProj (str, s) => (L'.StrProj (explifyStr str, s), loc)
adamc@45 207 | L.StrFun (m, n, dom, ran, str) => (L'.StrFun (m, n, explifySgn dom, explifySgn ran, explifyStr str), loc)
adamc@45 208 | L.StrApp (str1, str2) => (L'.StrApp (explifyStr str1, explifyStr str2), loc)
adamc@38 209 | L.StrError => raise Fail ("explifyStr: StrError at " ^ EM.spanToString loc)
adamc@38 210
adamc@88 211 val explify = List.mapPartial explifyDecl
adamc@38 212
adamc@38 213 end