adamc@25
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@25
|
2 * All rights reserved.
|
adamc@25
|
3 *
|
adamc@25
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@25
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@25
|
6 *
|
adamc@25
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@25
|
8 * this list of conditions and the following disclaimer.
|
adamc@25
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@25
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@25
|
11 * and/or other materials provided with the distribution.
|
adamc@25
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@25
|
13 * derived from this software without specific prior written permission.
|
adamc@25
|
14 *
|
adamc@25
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@25
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@25
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@25
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@25
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@25
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@25
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@25
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@25
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@25
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@25
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@25
|
26 *)
|
adamc@25
|
27
|
adamc@25
|
28 structure Monoize :> MONOIZE = struct
|
adamc@25
|
29
|
adamc@25
|
30 structure E = ErrorMsg
|
adamc@25
|
31 structure Env = CoreEnv
|
adamc@25
|
32
|
adamc@25
|
33 structure L = Core
|
adamc@25
|
34 structure L' = Mono
|
adamc@25
|
35
|
adamc@25
|
36 val dummyTyp = (L'.TNamed 0, E.dummySpan)
|
adamc@25
|
37
|
adamc@25
|
38 fun monoName env (all as (c, loc)) =
|
adamc@25
|
39 let
|
adamc@25
|
40 fun poly () =
|
adamc@25
|
41 (E.errorAt loc "Unsupported name constructor";
|
adamc@25
|
42 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
43 "")
|
adamc@25
|
44 in
|
adamc@25
|
45 case c of
|
adamc@25
|
46 L.CName s => s
|
adamc@25
|
47 | _ => poly ()
|
adamc@25
|
48 end
|
adamc@25
|
49
|
adamc@25
|
50 fun monoType env (all as (c, loc)) =
|
adamc@25
|
51 let
|
adamc@25
|
52 fun poly () =
|
adamc@25
|
53 (E.errorAt loc "Unsupported type constructor";
|
adamc@25
|
54 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
|
adamc@25
|
55 dummyTyp)
|
adamc@25
|
56 in
|
adamc@25
|
57 case c of
|
adamc@25
|
58 L.TFun (c1, c2) => (L'.TFun (monoType env c1, monoType env c2), loc)
|
adamc@25
|
59 | L.TCFun _ => poly ()
|
adamc@25
|
60 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
|
adamc@25
|
61 (L'.TRecord (map (fn (x, t) => (monoName env x, monoType env t)) xcs), loc)
|
adamc@25
|
62 | L.TRecord _ => poly ()
|
adamc@25
|
63
|
adamc@25
|
64 | L.CRel _ => poly ()
|
adamc@25
|
65 | L.CNamed n => (L'.TNamed n, loc)
|
adamc@51
|
66 | L.CFfi mx => (L'.TFfi mx, loc)
|
adamc@25
|
67 | L.CApp _ => poly ()
|
adamc@25
|
68 | L.CAbs _ => poly ()
|
adamc@25
|
69
|
adamc@25
|
70 | L.CName _ => poly ()
|
adamc@25
|
71
|
adamc@25
|
72 | L.CRecord _ => poly ()
|
adamc@25
|
73 | L.CConcat _ => poly ()
|
adamc@69
|
74 | L.CFold _ => poly ()
|
adamc@25
|
75 end
|
adamc@25
|
76
|
adamc@25
|
77 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
|
adamc@25
|
78
|
adamc@25
|
79 fun monoExp env (all as (e, loc)) =
|
adamc@25
|
80 let
|
adamc@25
|
81 fun poly () =
|
adamc@25
|
82 (E.errorAt loc "Unsupported expression";
|
adamc@25
|
83 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
|
adamc@25
|
84 dummyExp)
|
adamc@25
|
85 in
|
adamc@25
|
86 case e of
|
adamc@25
|
87 L.EPrim p => (L'.EPrim p, loc)
|
adamc@25
|
88 | L.ERel n => (L'.ERel n, loc)
|
adamc@25
|
89 | L.ENamed n => (L'.ENamed n, loc)
|
adamc@51
|
90 | L.EFfi mx => (L'.EFfi mx, loc)
|
adamc@51
|
91 | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp env) es), loc)
|
adamc@25
|
92 | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc)
|
adamc@26
|
93 | L.EAbs (x, dom, ran, e) =>
|
adamc@26
|
94 (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom) e), loc)
|
adamc@25
|
95 | L.ECApp _ => poly ()
|
adamc@25
|
96 | L.ECAbs _ => poly ()
|
adamc@25
|
97
|
adamc@29
|
98 | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x, monoExp env e, monoType env t)) xes), loc)
|
adamc@25
|
99 | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc)
|
adamc@73
|
100 | L.EFold _ => poly ()
|
adamc@25
|
101 end
|
adamc@25
|
102
|
adamc@25
|
103 fun monoDecl env (all as (d, loc)) =
|
adamc@25
|
104 let
|
adamc@25
|
105 fun poly () =
|
adamc@25
|
106 (E.errorAt loc "Unsupported declaration";
|
adamc@25
|
107 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
|
adamc@25
|
108 NONE)
|
adamc@25
|
109 in
|
adamc@25
|
110 case d of
|
adamc@25
|
111 L.DCon _ => NONE
|
adamc@25
|
112 | L.DVal (x, n, t, e) => SOME (Env.pushENamed env x n t (SOME e),
|
adamc@25
|
113 (L'.DVal (x, n, monoType env t, monoExp env e), loc))
|
adamc@25
|
114 end
|
adamc@25
|
115
|
adamc@25
|
116 fun monoize env ds =
|
adamc@25
|
117 let
|
adamc@25
|
118 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
|
adamc@25
|
119 case monoDecl env d of
|
adamc@25
|
120 NONE => (env, ds)
|
adamc@25
|
121 | SOME (env, d) => (env, d :: ds)) (env, []) ds
|
adamc@25
|
122 in
|
adamc@25
|
123 rev ds
|
adamc@25
|
124 end
|
adamc@25
|
125
|
adamc@25
|
126 end
|