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
|
module peval (main) where
import FilePath ( FilePath, (<.>), dropExtension
, replaceBaseName, takeBaseName, takeDirectory)
import Function ((***), first, second)
import List ((\\), find, nub)
import Text.Pretty ( Doc, ($$), (<$+$>), (<+>), char, compose
, pPrint, vsep, text)
import System (setEnviron)
import FlatCurry.Annotated.Goodies (unAnnFuncDecl)
import FlatCurry.Annotated.TypeInference (TypeEnv, inferNewFunctions)
import System.Console.ANSI.Codes ( green )
import Abstract (abstract)
import FlatCurry.Types
import FlatCurry.Files (readFlatCurry, writeFCY)
import FlatCurryGoodies (topSQ, sq, freeVars, funcsInExps, onBranchExps)
import FlatCurryPretty (ppExp, ppFuncDecls, ppProg, indent)
import NameChange (ncRenaming, ncResultants)
import Normalization (renameFuncDecl, renameResultant)
import Output (colorWith, info, status, detail, traceDetail, traceDebug)
import PeLetRW as LetRW (pevalExpr)
import PeNatural as Natural (pevalExpr)
import PeRLNT as RLNT (pevalExpr)
import PevalBase (Renaming, Resultant, ppRenaming, ppResultants)
import PevalOpts ( Options (..), Semantics (..)
, Abstraction (None), getOpts)
import PostUnfold (unAlias, postUnfold, removeCopies)
import Renaming (renameExpr)
import System.CurryPath (inCurrySubdir)
main :: IO ()
main = do
(opts, files) <- getOpts
mapIO_ (spec opts) files
spec :: Options -> FilePath -> IO ()
spec opts f = do
setEnviron "CURRYPATH" (takeDirectory f)
readFlatCurry (dropExtension f) >>= specPE opts >>= writeSpec opts f
writeSpec :: Options -> FilePath -> Prog -> IO ()
writeSpec opts f p = do
status opts $ "Writing specialized program into file '" ++ dest ++ "'."
writeFCY dest p
where dest = specFile opts f
specFile :: Options -> FilePath -> FilePath
specFile opts f = inCurrySubdir b' <.> "fcy"
where b' = replaceBaseName (dropExtension f) (takeBaseName f ++ optSuffix opts)
specPE :: Options -> Prog -> IO Prog
specPE opts p@(Prog m _ _ _ _) = do
let (prog, es) = extractPeval opts p
traceStep opts "Annotated Expressions" (vsep . map (indent . ppExp)) es
let peResult = pevalProg opts prog es
showDetail opts "Partial Evaluation Loop" peResult
traceInfo opts "Partially Evaluated (Sub)Expressions" ppPevals peResult
let (ren, res0) = createResultants m peResult
traceInfo opts "Independent Renaming" ppRenaming ren
traceInfo opts "Pre-Partial Evaluation" ppResultants res0
let res = renameResultants opts ren res0
typedResultants <- createFunctions p res
showDetail opts "Application of Renaming" res
traceInfo opts "Partial Evaluation" ppFuncDecls typedResultants
let (ren2, res2) = unAlias ren res
typedRes2 <- createFunctions p res2
traceInfo opts "Independent Renaming after Compression" ppRenaming ren2
traceInfo opts "Partial Evaluation after Compression" ppFuncDecls typedRes2
let (ren3, res3) = postUnfolding opts es ren2 res2
typedRes3 <- createFunctions p res3
showDetail opts "Compression of Resultants" typedRes3
traceInfo opts "Independent Renaming after Inlining" ppRenaming ren3
traceInfo opts "Partial Evaluation after Inlining" ppFuncDecls typedRes3
(ren4, res4) <- if optClosed opts
then return (ren3, res3)
else do
let (ren4', res4') = removeCopies prog ren3 res3
typedRes4 <- createFunctions p res4'
traceInfo opts "Independent Renaming after Copy Removal" ppRenaming ren4'
traceInfo opts "Partial Evaluation after Copy Removal" ppFuncDecls typedRes4
return (ren4', res4')
let (ren5, res5) = nameChange m ren4 res4
typedRes5 <- createFunctions p res5
traceInfo opts "Final Independent Renaming" ppRenaming ren5
traceStep opts "Final Partial Evaluation" ppFuncDecls typedRes5
let p' = integratePeval opts p ren5 typedRes5
showDetail opts "Renaming in original program" p'
traceInfo opts "Resulting program" ppProg p'
when (optDebug opts) $ putStrLn $ pPrint $ ppProg p'
return p'
ppPevals :: [(Expr, Expr)] -> Doc
ppPevals = compose (<$+$>) . map ppPeval
where ppPeval (l, r) = indent (ppExp l) $$ char '\8658' <+> indent (ppExp r)
traceStep :: Options -> String -> (a -> Doc) -> a -> IO ()
traceStep opts str pp x = showStatus opts str (pPrint . pp $!! x)
traceInfo :: Options -> String -> (a -> Doc) -> a -> IO ()
traceInfo opts str pp x = showInfo opts str (pPrint . pp $!! x)
showStatus :: Options -> String -> String -> IO ()
showStatus opts hdr cnt = status opts $
hdr ++ "\n" ++ replicate (length hdr) '-' ++ "\n" ++ cnt ++ "\n"
showInfo :: Options -> String -> String -> IO ()
showInfo opts hdr cnt = info opts $
hdr ++ "\n" ++ replicate (length hdr) '-' ++ "\n" ++ cnt ++ "\n"
showDetail :: Options -> String -> a -> IO ()
showDetail opts hdr val = detail opts $
hdr ++ "\n" ++ replicate (length hdr) '-' ++ "\n" ++ (const "" $!! val)
extractPeval :: Options -> Prog -> (Prog, [Expr])
opts (Prog m is ts oldfs ops) = (Prog m is ts newfs ops, concat es)
where (newfs, es) = unzip (map (extractFunc opts) oldfs)
extractFunc :: Options -> FuncDecl -> (FuncDecl, [Expr])
opts (Func n a v ty r) = first (Func n a v ty) (extractRule opts r)
extractRule :: Options -> Rule -> (Rule, [Expr])
_ e@(External _) = (e, [])
extractRule opts (Rule vs e) = first (Rule vs) (extractExpr opts e)
extractExpr :: Options -> Expr -> (Expr, [Expr])
_ v@(Var _) = (v, [])
extractExpr _ l@(Lit _) = (l, [])
extractExpr opts c@(Comb ct n es) = case getPevalTarget opts c of
Just e -> (e, [e])
Nothing -> (Comb ct n *** concat) (unzip (map (extractExpr opts) es))
extractExpr opts f@(Free vs e) = case getPevalTarget opts f of
Just fp -> (f, [fp])
Nothing -> first (Free vs) (extractExpr opts e)
extractExpr opts o@(Or e1 e2) = case getPevalTarget opts o of
Just fp -> (o, [fp])
Nothing -> let (ne1, pe1) = extractExpr opts e1
(ne2, pe2) = extractExpr opts e2
in (Or ne1 ne2, pe1 ++ pe2)
extractExpr opts (Case ct e bs) = let (ne , pe1) = extractExpr opts e
(nbs, pe2) = unzip (map getBranch bs)
in (Case ct ne nbs, concat (pe1 : pe2))
where getBranch (Branch p be) = first (Branch p) (extractExpr opts be)
extractExpr opts (Let bs e) = let (ne , pe1) = extractExpr opts e
(nbs, pe2) = unzip (map getBinding bs)
in (Let nbs ne, concat (pe1 : pe2))
where getBinding (v, ve) = first (\e' -> (v, e')) (extractExpr opts ve)
extractExpr opts (Typed e ty) = first (flip Typed ty) (extractExpr opts e)
getPevalTarget :: Options -> Expr -> Maybe Expr
getPevalTarget opts e = case e of
Comb FuncCall (_, "PEVAL") [e'] -> Just e'
_ | optFunPats opts && hasFP e -> Just e
| otherwise -> Nothing
hasFP :: Expr -> Bool
hasFP e = case e of
Comb FuncCall ("Prelude", "=:<=") _ -> True
Comb FuncCall ("Prelude", "&" ) [e1, _] -> hasFP e1
Comb FuncCall ("Prelude", "cond") [e1, _] -> hasFP e1
Free _ e1 -> hasFP e1
Or e1 e2 -> hasFP e1 || hasFP e2
_ -> False
pevalProg :: Options -> Prog -> [Expr] -> [(Expr, Expr)]
pevalProg opts p = reverse . pevalI [] . abstract opts p [] . map topSQ . reverse
where
pevalI cache es
| es == es' = cache'
| otherwise = pevalI cache' es'
where
cache' = foldl update cache es
es' = abstract opts p es (map snd cache' \\ map snd cache)
update cache e = case lookup e cache of
Nothing -> (e, tracePeval e) : cache
Just _ -> cache
tracePeval e = traceExp False "Evaluating expression" e
(traceDebug opts (colorWith opts green "using derivation")
(traceExp True "to result expression" e' $!! e'))
where
e' = pevalExpr opts p e
pevalExpr = case optSemantics opts of
RLNT -> RLNT.pevalExpr
Natural -> Natural.pevalExpr
LetRW -> LetRW.pevalExpr
traceExp nl str x = Output.traceDetail opts $ colorWith opts green
$ pPrint (indent (text str $$ ppExp x))
++ if nl then "\n" else ""
createResultants :: String -> [(Expr, Expr)] -> (Renaming, [Resultant])
createResultants m = unzip . zipWith create [0 ..] . filter madeProgress
where
madeProgress (e, e') = e /= e' && topSQ e /= e'
create n (e, e') = ((e, lhs), (lhs, e'))
where lhs = ((m, "_pe" ++ show n), freeVars e)
renameResultants :: Options -> Renaming -> [Resultant] -> [Resultant]
renameResultants opts ren = map (renameResultant . second (renameExpr opts ren))
postUnfolding :: Options -> [Expr] -> Renaming -> [Resultant]
-> (Renaming, [Resultant])
postUnfolding opts es ren rs = postUnfold opts fs ren rs
where fs = nub $ funcsInExps $ map (renameExpr opts ren . topSQ) es
nameChange :: String -> Renaming -> [Resultant] -> (Renaming, [Resultant])
nameChange mid ren res = (ncRenaming nc ren, ncResultants nc res)
where nc = zip (map (fst . fst) res) [(mid, "_pe" ++ show i) | i <- [0 ..]]
integratePeval :: Options -> Prog -> Renaming -> [FuncDecl] -> Prog
integratePeval opts (Prog m is ts fs ops) ren newfs = Prog m is ts fs' ops
where fs' = map (replaceFunc (opts, ren)) fs ++ newfs
replaceFunc :: (Options, Renaming) -> FuncDecl -> FuncDecl
replaceFunc env (Func n a v ty r) = Func n a v ty (replaceRule env r)
replaceRule :: (Options, Renaming) -> Rule -> Rule
replaceRule _ e@(External _) = e
replaceRule env (Rule vs e) = Rule vs (replace env e)
replace :: (Options, Renaming) -> Expr -> Expr
replace _ v@(Var _) = v
replace _ l@(Lit _) = l
replace env c@(Comb ct n es) = case getPevalTarget (fst env) c of
Just e' -> uncurry renameExpr env (topSQ e')
Nothing -> Comb ct n (map (replace env) es)
replace env f@(Free vs e) = case getPevalTarget (fst env) f of
Just e' -> uncurry renameExpr env (topSQ e')
Nothing -> Free vs (replace env e)
replace env o@(Or e1 e2) = case getPevalTarget (fst env) o of
Just e' -> uncurry renameExpr env (topSQ e')
Nothing -> Or (replace env e1) (replace env e2)
replace env (Case ct e bs) = Case ct (replace env e)
(replace env `onBranchExps` bs)
replace env (Let bs e) = Let (map (second (replace env)) bs)
(replace env e)
replace env (Typed e ty) = Typed (replace env e) ty
createFunctions :: Prog -> [Resultant] -> IO [FuncDecl]
createFunctions p rs = inferTypes p (map resultant2fundecl rs)
inferTypes :: Prog -> [FuncDecl] -> IO [FuncDecl]
inferTypes p fs = inferNewFunctions p fs >>= \res -> case res of
Left err -> error $ "Error during type inference: " ++ err
Right fs' -> return (map unAnnFuncDecl fs')
resultant2fundecl :: Resultant -> FuncDecl
resultant2fundecl ((f, vs), rhs)
= renameFuncDecl $ Func f arity Private ty (Rule vs rhs)
where
arity = length vs
ty = foldr1 FuncType $ map TVar [0 .. arity]
|