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
|
module Heap
( Binding (..), ppBinding
, Heap, ppHeap, emptyHeap, isEmptyHeap, elemHeap, getHeap, lookupHeap
, bindHole, bindExpr, bindFree, bindParam
, bindLazyExpr, bindLazyFree, bindLazyParam
, unbind, dereference, without
) where
import Function (on, second)
import List ((\\), nub, partition, sortBy)
import Text.Pretty (Doc, (<>), (<+>), char, listSpaced, text)
import FlatCurry.Types
import FlatCurryGoodies (freeVars, liftSQ, mkFree, mkLazyBind, mkLet, pat2exp
, topSQ, patVars, getSQ)
import FlatCurryPretty (ppExp, ppVarIndex)
data Binding
= BlackHole
| BoundVar Expr
| LazyBound Expr
| FreeVar
| LazyFree
| Param
| LazyParam
deriving Eq
ppBinding :: Binding -> Doc
ppBinding BlackHole = text "\9632"
ppBinding (BoundVar e) = ppExp e
ppBinding (LazyBound e) = text "~" <> ppExp e
ppBinding FreeVar = text "free"
ppBinding LazyFree = text "~free"
ppBinding Param = text "_"
ppBinding LazyParam = text "~_"
type Heap = [(VarIndex, Binding)]
ppHeap :: Heap -> Doc
ppHeap [] = text "[]"
ppHeap h@(_:_) = listSpaced (map ppVarBinding $ sortBy ((>=) `on` fst) h)
where ppVarBinding (v, b) = ppVarIndex v <+> char '\8614' <+> ppBinding b
emptyHeap :: Heap
emptyHeap = []
isEmptyHeap :: Heap -> Bool
isEmptyHeap = null
elemHeap :: VarIndex -> Heap -> Bool
elemHeap v h = v `elem` map fst h
getHeap :: VarIndex -> Heap -> Binding
getHeap v h = case lookup v h of
Just b -> b
Nothing -> Param
lookupHeap :: VarIndex -> Heap -> Maybe Binding
lookupHeap = lookup
bindHole :: VarIndex -> Heap -> Heap
bindHole v = bindHeap v BlackHole
bindExpr :: VarIndex -> Expr -> Heap -> Heap
bindExpr v e = bindHeap v (BoundVar e)
bindFree :: VarIndex -> Heap -> Heap
bindFree v = bindHeap v FreeVar
bindParam :: VarIndex -> Heap -> Heap
bindParam v = bindHeap v Param
bindLazyExpr :: VarIndex -> Expr -> Heap -> Heap
bindLazyExpr v e = bindHeap v (LazyBound e)
bindLazyFree :: VarIndex -> Heap -> Heap
bindLazyFree v = bindHeap v LazyFree
bindLazyParam :: VarIndex -> Heap -> Heap
bindLazyParam v = bindHeap v LazyParam
bindHeap :: VarIndex -> Binding -> Heap -> Heap
bindHeap v b h = (v, b) : unbind v h
unbind :: VarIndex -> Heap -> Heap
unbind v = filter ((/= v) . fst)
unbinds :: [VarIndex] -> Heap -> Heap
unbinds vs = filter ((`notElem` vs) . fst)
dereference :: Heap -> Expr -> Expr
dereference heap0 expr0 = snd (drf heap0 expr0)
where
drf :: Heap -> Expr -> ([VarIndex], Expr)
drf h expr = case expr of
Var v -> ([v], drfVar v h)
Lit _ -> ([] , expr )
Comb ct qn es -> case getSQ expr of
Just e -> let (vs, e') = drf h e in (vs, topSQ e')
_ -> let (shared, h' ) = splitShared h vss
(vss , es') = unzip $ map (drf h') es
in (nub (concat vss), addHeap shared (Comb ct qn es'))
Let ds e ->
let (shared, h' ) = splitShared h vss
(vs , es ) = unzip ds
(vss , (e':es')) = unzip $ map (drf (unbinds vs h')) (e:es)
in (nub (concat vss) \\ vs, addHeap shared (Let (zip vs es') e'))
Free vs e ->
let (vs', e') = drf (unbinds vs h) e
in (vs' \\ vs, Free vs e')
Or e1 e2 ->
let (shared, h' ) = splitShared h vss
(vss , [e1', e2']) = unzip $ map (drf h') [e1, e2]
in (nub (concat vss), addHeap shared (Or e1' e2'))
Case ct e bs ->
let (shared, h' ) = splitShared h vss'
(vs , e' ) = drf h' e
(vss , bs' ) = unzip $ map (drfBranch e' h') bs
vss' = [vs, nub $ concat vss]
in (nub (concat vss'), addHeap shared (Case ct e' bs'))
Typed e ty -> let (vs, e') = drf h e in (vs, Typed e' ty)
drfVar v h = case extract h [v] of
[(_, LazyBound e)] -> e
h' -> addHeap h' (Var v)
drfBranch :: Expr -> Heap -> BranchExpr -> ([VarIndex], BranchExpr)
drfBranch v h (Branch p e) = let (vs, e') = drf h' e
in (vs \\ patVars p, Branch p e')
where h' = case v of
Var x -> bindExpr x (pat2exp p) h
_ -> h
splitShared :: Heap -> [[VarIndex]] -> (Heap, Heap)
splitShared h vss = (sharedHeap, h \\ sharedHeap)
where
sharedHeap = extract h shared
shared = filter isShared $ nub $ concatMap (map fst) hs
isShared v = length (filter (elemHeap v) hs) > 1
hs = map (extract h) vss
addHeap :: Heap -> Expr -> Expr
addHeap h e
| isEmptyHeap h = e
| otherwise = liftSQ $ mkFree (getFrees h) $ mkLet (getBound h)
$ mkLazyBind (getLazyBound h) e
extract :: Heap -> [VarIndex] -> Heap
_ [] = []
extract h (v:vs) = case splitHeap v h of
(Nothing, _ ) -> extract h vs
(Just b , h') -> case b of
BoundVar e -> (v, b) : extract h' (vs ++ freeVars e)
LazyBound e -> (v, b) : extract h' (vs ++ freeVars e)
FreeVar -> (v, b) : extract h' vs
LazyFree -> (v, b) : extract h' vs
_ -> extract h' vs
splitHeap :: VarIndex -> Heap -> (Maybe Binding, Heap)
splitHeap _ [] = (Nothing, [])
splitHeap v ((w, b) : bs)
| v == w = (Just b, bs)
| otherwise = second ((w, b) :) (splitHeap v bs)
getFrees :: Heap -> [VarIndex]
getFrees h = [ v | (v, FreeVar) <- h ] ++ [ v | (v, LazyBound _) <- h]
getBound :: Heap -> [(VarIndex, Expr)]
getBound h = [ (v, e) | (v, BoundVar e) <- h ]
without :: Heap -> Heap -> Heap
without h1 h2 = [ b | b@(v, _) <- h1, v `notElem` map fst (getBound h2) ]
getLazyBound :: Heap -> [(VarIndex, Expr)]
getLazyBound h = [ (v, e) | (v, LazyBound e) <- h ]
|