adamc@133
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@133
|
2 * All rights reserved.
|
adamc@133
|
3 *
|
adamc@133
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@133
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@133
|
6 *
|
adamc@133
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@133
|
8 * this list of conditions and the following disclaimer.
|
adamc@133
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@133
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@133
|
11 * and/or other materials provided with the distribution.
|
adamc@133
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@133
|
13 * derived from this software without specific prior written permission.
|
adamc@133
|
14 *
|
adamc@133
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@133
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@133
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@133
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@133
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@133
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@133
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@133
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@133
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@133
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@133
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@133
|
26 *)
|
adamc@133
|
27
|
adamc@133
|
28 (* Simplify a Mono program algebraically *)
|
adamc@133
|
29
|
adamc@133
|
30 structure MonoReduce :> MONO_REDUCE = struct
|
adamc@133
|
31
|
adamc@133
|
32 open Mono
|
adamc@133
|
33
|
adamc@133
|
34 structure E = MonoEnv
|
adamc@133
|
35 structure U = MonoUtil
|
adamc@133
|
36
|
adamc@133
|
37
|
adamc@252
|
38 fun impure (e, _) =
|
adamc@252
|
39 case e of
|
adamc@252
|
40 EWrite _ => true
|
adamc@252
|
41 | EQuery _ => true
|
adamc@307
|
42 | EDml _ => true
|
adamc@252
|
43 | EAbs _ => false
|
adamc@252
|
44
|
adamc@252
|
45 | EPrim _ => false
|
adamc@252
|
46 | ERel _ => false
|
adamc@252
|
47 | ENamed _ => false
|
adamc@252
|
48 | ECon (_, _, eo) => (case eo of NONE => false | SOME e => impure e)
|
adamc@297
|
49 | ENone _ => false
|
adamc@290
|
50 | ESome (_, e) => impure e
|
adamc@252
|
51 | EFfi _ => false
|
adamc@252
|
52 | EFfiApp _ => false
|
adamc@252
|
53 | EApp ((EFfi _, _), _) => false
|
adamc@252
|
54 | EApp _ => true
|
adamc@252
|
55
|
adamc@252
|
56 | ERecord xes => List.exists (fn (_, e, _) => impure e) xes
|
adamc@252
|
57 | EField (e, _) => impure e
|
adamc@252
|
58
|
adamc@252
|
59 | ECase (e, pes, _) => impure e orelse List.exists (fn (_, e) => impure e) pes
|
adamc@252
|
60
|
adamc@283
|
61 | EError (e, _) => impure e
|
adamc@283
|
62
|
adamc@252
|
63 | EStrcat (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
64
|
adamc@252
|
65 | ESeq (e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
66 | ELet (_, _, e1, e2) => impure e1 orelse impure e2
|
adamc@252
|
67
|
adamc@252
|
68 | EClosure (_, es) => List.exists impure es
|
adamc@252
|
69
|
adamc@252
|
70
|
adamc@252
|
71 val liftExpInExp = Monoize.liftExpInExp
|
adamc@252
|
72
|
adamc@252
|
73 val subExpInExp' =
|
adamc@133
|
74 U.Exp.mapB {typ = fn t => t,
|
adamc@133
|
75 exp = fn (xn, rep) => fn e =>
|
adamc@133
|
76 case e of
|
adamc@133
|
77 ERel xn' =>
|
adamc@133
|
78 (case Int.compare (xn', xn) of
|
adamc@133
|
79 EQUAL => #1 rep
|
adamc@133
|
80 | GREATER=> ERel (xn' - 1)
|
adamc@133
|
81 | LESS => e)
|
adamc@133
|
82 | _ => e,
|
adamc@133
|
83 bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep)
|
adamc@133
|
84 | (ctx, _) => ctx}
|
adamc@133
|
85
|
adamc@252
|
86 fun subExpInExp (n, e1) e2 =
|
adamc@252
|
87 let
|
adamc@252
|
88 val r = subExpInExp' (n, e1) e2
|
adamc@252
|
89 in
|
adamc@252
|
90 (*Print.prefaces "subExpInExp" [("e1", MonoPrint.p_exp MonoEnv.empty e1),
|
adamc@252
|
91 ("e2", MonoPrint.p_exp MonoEnv.empty e2),
|
adamc@252
|
92 ("r", MonoPrint.p_exp MonoEnv.empty r)];*)
|
adamc@252
|
93 r
|
adamc@252
|
94 end
|
adamc@133
|
95
|
adamc@133
|
96 fun typ c = c
|
adamc@133
|
97
|
adamc@316
|
98 val swapExpVars =
|
adamc@316
|
99 U.Exp.mapB {typ = fn t => t,
|
adamc@316
|
100 exp = fn lower => fn e =>
|
adamc@316
|
101 case e of
|
adamc@316
|
102 ERel xn =>
|
adamc@316
|
103 if xn = lower then
|
adamc@316
|
104 ERel (lower + 1)
|
adamc@316
|
105 else if xn = lower + 1 then
|
adamc@316
|
106 ERel lower
|
adamc@316
|
107 else
|
adamc@316
|
108 e
|
adamc@316
|
109 | _ => e,
|
adamc@316
|
110 bind = fn (lower, U.Exp.RelE _) => lower+1
|
adamc@316
|
111 | (lower, _) => lower}
|
adamc@316
|
112
|
adamc@258
|
113 datatype result = Yes of E.env | No | Maybe
|
adamc@258
|
114
|
adamc@183
|
115 fun match (env, p : pat, e : exp) =
|
adamc@183
|
116 case (#1 p, #1 e) of
|
adamc@258
|
117 (PWild, _) => Yes env
|
adamc@258
|
118 | (PVar (x, t), _) => Yes (E.pushERel env x t (SOME e))
|
adamc@183
|
119
|
adamc@280
|
120 | (PPrim (Prim.String s), EStrcat ((EPrim (Prim.String s'), _), _)) =>
|
adamc@280
|
121 if String.isPrefix s' s then
|
adamc@280
|
122 Maybe
|
adamc@280
|
123 else
|
adamc@280
|
124 No
|
adamc@280
|
125
|
adamc@183
|
126 | (PPrim p, EPrim p') =>
|
adamc@183
|
127 if Prim.equal (p, p') then
|
adamc@258
|
128 Yes env
|
adamc@183
|
129 else
|
adamc@258
|
130 No
|
adamc@183
|
131
|
adamc@188
|
132 | (PCon (_, PConVar n1, NONE), ECon (_, PConVar n2, NONE)) =>
|
adamc@183
|
133 if n1 = n2 then
|
adamc@258
|
134 Yes env
|
adamc@183
|
135 else
|
adamc@258
|
136 No
|
adamc@183
|
137
|
adamc@188
|
138 | (PCon (_, PConVar n1, SOME p), ECon (_, PConVar n2, SOME e)) =>
|
adamc@183
|
139 if n1 = n2 then
|
adamc@183
|
140 match (env, p, e)
|
adamc@183
|
141 else
|
adamc@258
|
142 No
|
adamc@183
|
143
|
adamc@188
|
144 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, NONE), ECon (_, PConFfi {mod = m2, con = con2, ...}, NONE)) =>
|
adamc@185
|
145 if m1 = m2 andalso con1 = con2 then
|
adamc@258
|
146 Yes env
|
adamc@185
|
147 else
|
adamc@258
|
148 No
|
adamc@185
|
149
|
adamc@188
|
150 | (PCon (_, PConFfi {mod = m1, con = con1, ...}, SOME ep), ECon (_, PConFfi {mod = m2, con = con2, ...}, SOME e)) =>
|
adamc@185
|
151 if m1 = m2 andalso con1 = con2 then
|
adamc@185
|
152 match (env, p, e)
|
adamc@185
|
153 else
|
adamc@258
|
154 No
|
adamc@185
|
155
|
adamc@183
|
156 | (PRecord xps, ERecord xes) =>
|
adamc@183
|
157 let
|
adamc@183
|
158 fun consider (xps, env) =
|
adamc@183
|
159 case xps of
|
adamc@258
|
160 [] => Yes env
|
adamc@183
|
161 | (x, p, _) :: rest =>
|
adamc@183
|
162 case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@258
|
163 NONE => No
|
adamc@183
|
164 | SOME (_, e, _) =>
|
adamc@183
|
165 case match (env, p, e) of
|
adamc@258
|
166 No => No
|
adamc@258
|
167 | Maybe => Maybe
|
adamc@258
|
168 | Yes env => consider (rest, env)
|
adamc@183
|
169 in
|
adamc@183
|
170 consider (xps, env)
|
adamc@183
|
171 end
|
adamc@183
|
172
|
adamc@258
|
173 | _ => Maybe
|
adamc@183
|
174
|
adamc@133
|
175 fun exp env e =
|
adamc@133
|
176 case e of
|
adamc@183
|
177 ERel n =>
|
adamc@183
|
178 (case E.lookupERel env n of
|
adamc@183
|
179 (_, _, SOME e') => #1 e'
|
adamc@183
|
180 | _ => e)
|
adamc@183
|
181 | ENamed n =>
|
adamc@133
|
182 (case E.lookupENamed env n of
|
adamc@133
|
183 (_, _, SOME e', _) => #1 e'
|
adamc@133
|
184 | _ => e)
|
adamc@133
|
185
|
adamc@252
|
186 | EApp ((EAbs (x, t, _, e1), loc), e2) =>
|
adamc@252
|
187 ((*Print.prefaces "Considering" [("e1", MonoPrint.p_exp env e1),
|
adamc@252
|
188 ("e2", MonoPrint.p_exp env e2)];*)
|
adamc@252
|
189 if impure e2 then
|
adamc@252
|
190 #1 (reduceExp env (ELet (x, t, e2, e1), loc))
|
adamc@252
|
191 else
|
adamc@252
|
192 #1 (reduceExp env (subExpInExp (0, e2) e1)))
|
adamc@133
|
193
|
adamc@184
|
194 | ECase (disc, pes, _) =>
|
adamc@258
|
195 let
|
adamc@258
|
196 fun search pes =
|
adamc@258
|
197 case pes of
|
adamc@258
|
198 [] => e
|
adamc@258
|
199 | (p, body) :: pes =>
|
adamc@258
|
200 case match (env, p, disc) of
|
adamc@258
|
201 No => search pes
|
adamc@258
|
202 | Maybe => e
|
adamc@258
|
203 | Yes env => #1 (reduceExp env body)
|
adamc@258
|
204 in
|
adamc@258
|
205 search pes
|
adamc@258
|
206 end
|
adamc@183
|
207
|
adamc@252
|
208 | EField ((ERecord xes, _), x) =>
|
adamc@252
|
209 (case List.find (fn (x', _, _) => x' = x) xes of
|
adamc@252
|
210 SOME (_, e, _) => #1 e
|
adamc@252
|
211 | NONE => e)
|
adamc@252
|
212
|
adamc@252
|
213 | ELet (x1, t1, (ELet (x2, t2, e1, b1), loc), b2) =>
|
adamc@252
|
214 let
|
adamc@252
|
215 val e' = (ELet (x2, t2, e1,
|
adamc@252
|
216 (ELet (x1, t1, b1,
|
adamc@252
|
217 liftExpInExp 1 b2), loc)), loc)
|
adamc@252
|
218 in
|
adamc@253
|
219 (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)),
|
adamc@253
|
220 ("e'", MonoPrint.p_exp env e')];*)
|
adamc@252
|
221 #1 (reduceExp env e')
|
adamc@252
|
222 end
|
adamc@252
|
223 | EApp ((ELet (x, t, e, b), loc), e') =>
|
adamc@252
|
224 #1 (reduceExp env (ELet (x, t, e,
|
adamc@252
|
225 (EApp (b, liftExpInExp 0 e'), loc)), loc))
|
adamc@316
|
226
|
adamc@316
|
227 | ELet (x, t, e, (EAbs (x', t' as (TRecord [], _), ran, e'), loc)) =>
|
adamc@316
|
228 EAbs (x', t', ran, (ELet (x, t, liftExpInExp 0 e, swapExpVars 0 e'), loc))
|
adamc@316
|
229
|
adamc@252
|
230 | ELet (x, t, e', b) =>
|
adamc@252
|
231 if impure e' then
|
adamc@252
|
232 e
|
adamc@252
|
233 else
|
adamc@252
|
234 #1 (reduceExp env (subExpInExp (0, e') b))
|
adamc@252
|
235
|
adamc@268
|
236 | EStrcat ((EPrim (Prim.String s1), _), (EPrim (Prim.String s2), _)) =>
|
adamc@268
|
237 EPrim (Prim.String (s1 ^ s2))
|
adamc@268
|
238
|
adamc@133
|
239 | _ => e
|
adamc@133
|
240
|
adamc@252
|
241 and bind (env, b) =
|
adamc@252
|
242 case b of
|
adamc@252
|
243 U.Decl.Datatype (x, n, xncs) => E.pushDatatype env x n xncs
|
adamc@252
|
244 | U.Decl.RelE (x, t) => E.pushERel env x t NONE
|
adamc@252
|
245 | U.Decl.NamedE (x, n, t, eo, s) => E.pushENamed env x n t (Option.map (reduceExp env) eo) s
|
adamc@252
|
246
|
adamc@133
|
247 and reduceExp env = U.Exp.mapB {typ = typ, exp = exp, bind = bind} env
|
adamc@133
|
248
|
adamc@133
|
249 fun decl env d = d
|
adamc@133
|
250
|
adamc@133
|
251 val reduce = U.File.mapB {typ = typ, exp = exp, decl = decl, bind = bind} E.empty
|
adamc@133
|
252
|
adamc@133
|
253 end
|