comparison src/monoize.sml @ 25:0a762c73824d

Monoize
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Jun 2008 13:14:45 -0400
parents
children 4ab19c19665f
comparison
equal deleted inserted replaced
24:ea15905e598d 25:0a762c73824d
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 Monoize :> MONOIZE = struct
29
30 structure E = ErrorMsg
31 structure Env = CoreEnv
32
33 structure L = Core
34 structure L' = Mono
35
36 val dummyTyp = (L'.TNamed 0, E.dummySpan)
37
38 fun monoName env (all as (c, loc)) =
39 let
40 fun poly () =
41 (E.errorAt loc "Unsupported name constructor";
42 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
43 "")
44 in
45 case c of
46 L.CName s => s
47 | _ => poly ()
48 end
49
50 fun monoType env (all as (c, loc)) =
51 let
52 fun poly () =
53 (E.errorAt loc "Unsupported type constructor";
54 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
55 dummyTyp)
56 in
57 case c of
58 L.TFun (c1, c2) => (L'.TFun (monoType env c1, monoType env c2), loc)
59 | L.TCFun _ => poly ()
60 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
61 (L'.TRecord (map (fn (x, t) => (monoName env x, monoType env t)) xcs), loc)
62 | L.TRecord _ => poly ()
63
64 | L.CRel _ => poly ()
65 | L.CNamed n => (L'.TNamed n, loc)
66 | L.CApp _ => poly ()
67 | L.CAbs _ => poly ()
68
69 | L.CName _ => poly ()
70
71 | L.CRecord _ => poly ()
72 | L.CConcat _ => poly ()
73 end
74
75 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
76
77 fun monoExp env (all as (e, loc)) =
78 let
79 fun poly () =
80 (E.errorAt loc "Unsupported expression";
81 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
82 dummyExp)
83 in
84 case e of
85 L.EPrim p => (L'.EPrim p, loc)
86 | L.ERel n => (L'.ERel n, loc)
87 | L.ENamed n => (L'.ENamed n, loc)
88 | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc)
89 | L.EAbs (x, t, e) =>
90 (L'.EAbs (x, monoType env t, monoExp (Env.pushERel env x t) e), loc)
91 | L.ECApp _ => poly ()
92 | L.ECAbs _ => poly ()
93
94 | L.ERecord xes => (L'.ERecord (map (fn (x, e) => (monoName env x, monoExp env e)) xes), loc)
95 | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc)
96 end
97
98 fun monoDecl env (all as (d, loc)) =
99 let
100 fun poly () =
101 (E.errorAt loc "Unsupported declaration";
102 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
103 NONE)
104 in
105 case d of
106 L.DCon _ => NONE
107 | L.DVal (x, n, t, e) => SOME (Env.pushENamed env x n t (SOME e),
108 (L'.DVal (x, n, monoType env t, monoExp env e), loc))
109 end
110
111 fun monoize env ds =
112 let
113 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
114 case monoDecl env d of
115 NONE => (env, ds)
116 | SOME (env, d) => (env, d :: ds)) (env, []) ds
117 in
118 rev ds
119 end
120
121 end