1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
module Normalization
(
eqNorm, eqRen, normalizeExpr, normalizeFreeExpr, simplifyExpr
, freshResultant, freshRule
, renameResultant, renameFuncDecl, renameFreeExpr, renameExpr, renameExprSubst
) where
import Function (second)
import List (intersect, mapAccumL, partition)
import Utils (count, sameLength)
import FlatCurry.Types
import FlatCurryGoodies ( addPartCallArg, eqPattern, failedExpr, freeVars
, freeVarsDup, maxVar, isLit, topSQ, getSQ
, func, isConsCall, isPartCall, isVar, maxVarIndex
, maximumVarIndex, isSQ, isFailed, findBranch
, mkCase, mkFree, mkLet, mkOr, patVars, sq
, prelApply, prelCond, prelCond', trExpr)
import Output (assert)
import PevalBase (Resultant)
import Subst (mkSubst, singleSubst, subst, varSubst)
eqNorm :: Expr -> Expr -> Bool
eqNorm x y = normalizeExpr x == normalizeExpr y
normalizeExpr :: Expr -> Expr
normalizeExpr = renameExpr . orderDecls
normalizeFreeExpr :: Expr -> Expr
normalizeFreeExpr = renameFreeExpr . orderDecls
renameFreeExpr :: Expr -> Expr
renameFreeExpr e = freshExpr (maxVar (freeVars e) + 1) e
renameExprSubst :: Expr -> (Expr, [(VarIndex, VarIndex)])
renameExprSubst e
= assert (uncurry (flip varSubst) (unzip sub) e' `eqRen` e) "renameExprSubst"
(e', sub)
where
e' = snd $ rnExpr sub (newVars $ maxVar fvs + 1) e
fvs = freeVars e
sub = zip fvs [-1, -2 ..]
freshResultant :: VarIndex -> Resultant -> Resultant
freshResultant i ((f, vs), e) = ((f, vs'), e')
where (Rule vs' e') = snd $ rnRule (newVars i) (Rule vs e)
freshPattern :: VarIndex -> Pattern -> Pattern
freshPattern i p = snd (rnPattern (newVars i) p)
freshRule :: VarIndex -> Rule -> Rule
freshRule i = snd . rnRule (newVars i)
freshExpr :: VarIndex -> Expr -> Expr
freshExpr i = snd . rnExpr [] (newVars i)
renameResultant :: Resultant -> Resultant
renameResultant = freshResultant 1
renameFuncDecl :: FuncDecl -> FuncDecl
renameFuncDecl = snd . rnFunc (newVars 1)
eqRen :: Expr -> Expr -> Bool
eqRen x y = renameExpr x == renameExpr y
renameExpr :: Expr -> Expr
renameExpr e = snd $ rnExpr (zip (freeVars e) [-1, -2 ..]) (newVars 1) e
newVars :: VarIndex -> [VarIndex]
newVars x = [ x .. ]
takeVars :: [VarIndex] -> [a] -> ([VarIndex], [VarIndex])
takeVars fresh [] = (fresh, [])
takeVars (f:fs) (_:os) = let (fs', os') = takeVars fs os in (fs', f : os')
takeVars [] (_:_) = error "Renaming.takeVars: no more fresh variables"
type Renaming a = [VarIndex] -> a -> ([VarIndex], a)
rnProg :: Renaming Prog
rnProg xs (Prog m is ty fs os) = (xs1, Prog m is ty fs' os)
where (xs1, fs') = mapAccumL rnFunc xs fs
rnFunc :: Renaming FuncDecl
rnFunc xs (Func f a v ty r) = second (Func f a v ty) (rnRule xs r)
rnRule :: Renaming Rule
rnRule xs (Rule vs e) = let (xs1, vs') = takeVars xs vs
(xs2, e' ) = rnExpr (zip vs vs') xs1 e
in (xs2, Rule vs' e')
rnRule xs (External s) = (xs, External s)
rnExpr :: [(VarIndex, VarIndex)] -> Renaming Expr
rnExpr ren xs (Var x) = case lookup x ren of
Nothing -> (xs, Var x)
Just w -> (xs, Var w)
rnExpr _ xs l@(Lit _) = (xs, l)
rnExpr ren xs (Comb ct qn es)
= let (xs1, es') = mapAccumL (rnExpr ren) xs es
in (xs1, Comb ct qn es')
rnExpr ren xs (Free vs e)
= let (xs1, vs') = takeVars xs vs
ren1 = zip vs vs' ++ filter ((`notElem` vs) . fst) ren
(xs2, e' ) = rnExpr ren1 xs1 e
in (xs2, Free vs' e')
rnExpr ren xs (Let ds e)
= let (vs , es ) = unzip ds
(xs1, vs') = takeVars xs ds
ren1 = zip vs vs' ++ filter ((`notElem` vs) . fst) ren
(xs2, es') = mapAccumL (rnExpr ren1) xs1 es
(xs3, e' ) = rnExpr ren1 xs2 e
in (xs3, Let (zip vs' es') e')
rnExpr ren xs (Or e1 e2)
= let (xs1, e1') = rnExpr ren xs e1
(xs2, e2') = rnExpr ren xs1 e2
in (xs2, Or e1' e2')
rnExpr ren xs (Case ct e bs)
= let (xs1, e' ) = rnExpr ren xs e
(xs2, bs') = mapAccumL (rnBranchExpr ren) xs1 bs
in (xs2, Case ct e' bs')
rnExpr ren xs (Typed e ty)
= let (xs1, e') = rnExpr ren xs e
in (xs1, Typed e' ty)
rnBranchExpr :: [(VarIndex, VarIndex)] -> Renaming BranchExpr
rnBranchExpr ren ys (Branch (Pattern p zs) be)
= let (ys1, zs') = takeVars ys zs
ren1 = zip zs zs' ++ filter ((`notElem` zs) . fst) ren
(ys2, be') = rnExpr ren1 ys1 be
in (ys2, Branch (Pattern p zs') be')
rnBranchExpr ren ys (Branch l@(LPattern _) be)
= let (ys1, be') = rnExpr ren ys be
in (ys1, Branch l be')
rnPattern :: Renaming Pattern
rnPattern xs (Pattern p ys) = second (Pattern p) (takeVars xs ys)
rnPattern xs l@(LPattern _) = (xs, l)
orderDecls :: Expr -> Expr
orderDecls = trExpr Var Lit Comb oFree Or Case Branch oLet Typed
where
oFree vs e = let vs' = filter (`elem` vs) (freeVars e)
in assert (sameLength vs vs') "orderDecls"
mkFree vs' e
oLet ds e = mkLet (extract ds (freeVars e)) e
[] _ = []
extract (_:_) [] = error "Normalization.orderDecls"
extract ds@(_:_) (v:vs) = case break ((== v) . fst) ds of
(_ , [] ) -> extract ds vs
(ds1, (_, e'):ds2) -> (v, e') : extract (ds1 ++ ds2) (vs ++ freeVars e')
simplifyExpr :: Expr -> Expr
simplifyExpr = trExpr Var Lit cComb cFree cOr mkCase Branch cLet Typed
where
cComb ct f es = let call = Comb ct f es in case getSQ call of
Just e -> sq e
_ -> call
cFree vs e = mkFree (vs `intersect` freeVars e) e
cOr e1 e2 = case (e1, e2) of
(Comb FuncCall f [a, b], Comb FuncCall g [c, d])
| all (`elem` [prelCond, prelCond']) [f, g]
&& a == c -> simplifyExpr $ func prelCond [a, Or b d]
(Case ct1 e1' bs1, Case ct2 e2' bs2)
| ct1 == ct2 && e1' == e2' -> simplifyExpr $ Case ct1 e1'
(mergeBranches bs1 bs2)
_ | isSQ e1 || isSQ e2 -> simplifyExpr (topSQ (Or e1 e2))
| otherwise -> mkOr e1 e2
mergeBranches [] bs2 = bs2
mergeBranches (b@(Branch p1 e1):bs1) bs2
= case break (\(Branch p' _) -> eqPattern p1 p') bs2 of
(_ , [] ) -> b : mergeBranches bs1 bs2
(bs21, Branch p2 e2 : bs22) ->
let p' = freshPattern (maximumVarIndex [e1, e2]) p1
e' = Or (varSubst (patVars p1) (patVars p') e1)
(varSubst (patVars p2) (patVars p') e2)
in Branch p' (simplifyExpr e') : mergeBranches bs1 (bs21 ++ bs22)
cLet ds e
| ds == ds' && e == e' = mkLet ds' e'
| otherwise = cLet ds' e'
where
(ds', e') = cLet' [] ds
cLet' bs0 [] = (bs0, e)
cLet' bs1 (b : bs2)
| isInlineable b = (map (second replace) (bs1 ++ bs2), replace e)
| otherwise = cLet' (bs1 ++ [b]) bs2
where
isInlineable bd = uncurry isSimple bd || not (isShared bd)
isShared (v, _ ) = count v (concatMap freeVarsDup (e : map snd ds)) > 1
replace = simplifyExpr . subst (uncurry singleSubst b)
isSimple v ve = case ve of
Var x -> x /= v
Lit _ -> True
Comb ct _ es -> (ct == ConsCall || isPartCall ct) && all (isSimple v) es
_ -> isFailed ve
cCase ct e bs = case e of
Lit l -> case findBranch (LPattern l) bs of
Nothing -> failedExpr
Just ( _, be) -> be
Comb ConsCall c es -> case findBranch (Pattern c []) bs of
Nothing -> failedExpr
Just (xs, be) -> simplifyExpr (unfold xs es be)
_ -> mkCase ct e bs
unfold :: [VarIndex] -> [Expr] -> Expr -> Expr
unfold vs es e = mkLet (zip vs' es) e'
where Rule vs' e' = freshRule (maximumVarIndex es + 1) (Rule vs e)
type Flattening a = [VarIndex] -> a -> ([VarIndex], a)
flattenProg :: Flattening Prog
flattenProg xs (Prog m is ty fs os) = (xs1, Prog m is ty fs' os)
where (xs1, fs') = mapAccumL flattenFunc xs fs
flattenFunc :: Flattening FuncDecl
flattenFunc xs (Func f a v ty r) = second (Func f a v ty) (flattenRule xs r)
flattenRule :: Flattening Rule
flattenRule xs (Rule vs e) = second (Rule vs) (flattenExpr xs e)
flattenRule xs (External s) = (xs, External s)
flattenExpr :: Flattening Expr
flattenExpr xs v@(Var _) = (xs, v)
flattenExpr xs l@(Lit _) = (xs, l)
flattenExpr xs (Comb ct qn es) = let (xs1, ds, es') = splitArgs xs es
in (xs1, flatLet ds (Comb ct qn es'))
flattenExpr xs (Free vs e) = let (xs1, e' ) = flattenExpr xs e
in (xs1, Free vs e')
flattenExpr xs (Let ds e) = let (xs1, ds') = mapAccumL flatD xs ds
(xs2, e' ) = flattenExpr xs1 e
in (xs2, flatLet ds' e')
where flatD ys (v, ve) = let (ys1, ve') = flattenExpr ys ve
in (ys1, (v, ve'))
flattenExpr xs (Or e1 e2) = let (xs1, e1') = flattenExpr xs e1
(xs2, e2') = flattenExpr xs1 e2
in (xs2, Or e1' e2')
flattenExpr xs (Case ct e bs) = let (xs1, e' ) = flattenExpr xs e
(xs2, bs') = mapAccumL flatB xs1 bs
in (xs2, Case ct e' bs')
where flatB ys (Branch p be) = let (ys1, be') = flattenExpr ys be
in (ys1, Branch p be')
flattenExpr xs (Typed e ty) = let (xs1, e' ) = flattenExpr xs e
in (xs1, Typed e' ty)
splitArgs :: [VarIndex] -> [Expr] -> ([VarIndex], [(VarIndex, Expr)], [Expr])
splitArgs xs [] = (xs, [], [])
splitArgs xs (e:es) = case e of
Var _ -> let (xs', ds, es') = splitArgs xs es
in (xs', ds, e : es')
_ -> let (x' : xs', e') = flattenExpr xs e
(xs2, ds, es') = splitArgs xs' es
in (xs2, (x', e') : ds, Var x' : es')
flatLet :: [(VarIndex, Expr)] -> Expr -> Expr
flatLet decls ex = case liftDecls decls of
[] -> ex
ds' -> case ex of
Let ds'' e' -> Let (ds' ++ ds'') e'
_ -> Let ds' ex
where
liftDecls [] = []
liftDecls ((x, d) : ds) = case d of
Let ds1 e -> ds1 ++ (x, e) : liftDecls ds
_ -> (x, d) : liftDecls ds
|