comparison src/corify.sml @ 16:bc7b76ca57e0

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