forked from takano-akio/ww-fusion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWWFusion.hs
324 lines (286 loc) · 7.39 KB
/
WWFusion.hs
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
{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
module WWFusion
( foldrW
, buildW
, foldl
, foldl'
, foldr
, filter
, map
, eft
, (++)
, concat
, dropWhile
, reverse
, scanl
, Wrap(..)
) where
import Prelude hiding ((++), foldl, foldr, concat, filter, map, reverse, dropWhile, scanl)
data Wrap f b = Wrap (forall e. f e -> e -> b -> b) (forall e. (e -> b -> b) -> f e)
foldrW
:: Wrap f b
-> (a -> b -> b)
-> b
-> [a]
-> b
foldrW (Wrap wrap unwrap) f z0 list0 = wrap go list0 z0
where
go = unwrap $ \list z' -> case list of
[] -> z'
x:xs -> f x $ wrap go xs z'
{-# NOINLINE[0] foldrW #-}
newtype Simple b e = Simple { runSimple :: e -> b -> b }
isoSimple :: Wrap (Simple b) b
isoSimple = Wrap runSimple Simple
static :: b -> Wrap (Simple b) (b -> b -> b)
static z0 = Wrap
(\(Simple f) e k z b -> k (f e b) z)
(\u -> Simple $ \e i -> u e const z0 i)
staticCons :: (a -> r -> r) -> a -> (r -> r -> r) -> (r -> r -> r)
staticCons c = \x k l r -> c x (k l r)
{-# INLINE staticCons #-}
-- foldr :: (a -> b -> b) -> b -> [a] -> b
-- foldr f z = foldrW isoSimple f z
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f z = \xs -> foldrW (static z) (staticCons f) const xs z z
{-# INLINE foldr #-}
buildW
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
buildW g = g isoSimple (:) []
{-# INLINE[0] buildW #-}
buildWStatic
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
buildWStatic g = g (static []) (staticCons (:)) const [] []
{-# INLINE[0] buildWStatic #-}
augmentW
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
-> [a]
augmentW g xs = g isoSimple (:) xs
{-# INLINE[0] augmentW #-}
augmentWStatic
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
-> [a]
augmentWStatic g xs = g (static xs) (staticCons (:)) const xs xs
{-# INLINE[0] augmentWStatic #-}
(++) :: [a] -> [a] -> [a]
a ++ b = augmentW (\i c n -> foldrW i c n a) b
{-# INLINE (++) #-}
concat :: [[a]] -> [a]
concat xs = buildWStatic (\i c n -> foldrW i (\x y -> foldrW i c y x) n xs)
{-# INLINE concat #-}
foldl' :: (b -> a -> b) -> b -> [a] -> b
foldl' f initial = \xs -> foldrW (Wrap wrap unwrap) g id xs initial
where
wrap (Simple s) e k a = k $ s e a
unwrap u = Simple $ \e a -> u e id a
g x next acc = next $! f acc x
{-# INLINE foldl' #-}
foldl :: (b -> a -> b) -> b -> [a] -> b
foldl f initial = \xs -> foldrW wrapFoldl g id xs initial
where
g x next acc = next $ f acc x
{-# INLINE foldl #-}
wrapFoldl :: Wrap (Simple b) (b -> b)
wrapFoldl = Wrap (\(Simple s) e k a -> k $ s e a)
(\u -> Simple $ \e a -> u e id a)
map :: (a -> b) -> [a] -> [b]
map f = \xs -> buildWStatic (mapFB f xs)
{-# INLINE map #-}
mapFB
:: (a -> b)
-> [a]
-> Wrap f r
-> (b -> r -> r)
-> r
-> r
mapFB f xs = \ww cons nil -> foldrW ww (cons . f) nil xs
{-# INLINE mapFB #-}
filter :: (a -> Bool) -> [a] -> [a]
filter p = \xs -> buildWStatic (filterFB p xs)
{-# INLINE filter #-}
eft :: Int -> Int -> [Int]
eft = \from to -> buildWStatic (eftFB from to)
{-# INLINE eft #-}
eftFB
:: Int
-> Int
-> (Wrap f r)
-> (Int -> r -> r)
-> r
-> r
eftFB from to (Wrap wrap unwrap) cons nil = wrap go from nil
where
go = unwrap $ \i rest -> if i <= to
then cons i $ wrap go (i + 1) rest
else rest
{-# INLINE[0] eftFB #-}
filterFB
:: (a -> Bool)
-> [a]
-> (Wrap f r)
-> (a -> r -> r)
-> r
-> r
filterFB p xs ww cons nil = foldrW ww f nil xs
where
f x y = if p x then cons x y else y
{-# INLINE[0] filterFB #-}
dropWhile :: (a -> Bool) -> [a] -> [a]
dropWhile p xs = buildW $ dwFB p xs
{-# INLINE dropWhile #-}
dwFB :: (a -> Bool) -> [a] -> Wrap f r -> (a -> r -> r) -> r -> r
dwFB p xs = \w cons nil -> foldrW (dwWrap w) (dwCons p cons) (dwNil nil) xs True
{-# INLINE dwFB #-}
newtype Env r f e = Env { runEnv :: r -> f e }
newtype Arg s f e = Arg { runArg :: f (e, s) }
dwWrap :: Wrap f r -> Wrap (Arg s f) (s -> r)
dwWrap (Wrap wrap unwrap) = Wrap
(\(Arg h) e k s -> wrap h (e,s) (k s))
(\h -> Arg . unwrap $ \(e,s) r -> h e (\_ -> r) s)
{-# INLINE[0] dwWrap #-}
dwNil :: r -> Bool -> r
dwNil r _ = r
{-# INLINE[0] dwNil #-}
dwCons :: (a -> Bool) -> (a -> r -> r) -> a -> (Bool -> r) -> (Bool -> r)
dwCons p c = \e k b -> let b' = b && p e in if b' then k b' else e `c` k b'
{-# INLINE[0] dwCons #-}
reverse :: [a] -> [a]
reverse xs = buildW $ revFB xs
{-# INLINE reverse #-}
revFB :: [a] -> Wrap f r -> (a -> r -> r) -> r -> r
revFB xs = \w cons nil -> foldrW (revWrap w) (revCons cons) id xs nil
{-# INLINE revFB #-}
revWrap :: Wrap f r -> Wrap f (r -> r)
revWrap (Wrap wrap unwrap) = Wrap
(\h e k s -> k $ wrap h e s)
(\h -> unwrap $ \e r -> h e id r)
{-# INLINE[0] revWrap #-}
revCons :: (a -> r -> r) -> a -> (r -> r) -> r -> r
revCons c e k z = k (c e z)
{-# INLINE[0] revCons #-}
scanl :: (b -> a -> b) -> b -> [a] -> [b]
scanl f z xs = buildW (scanlFB f z xs)
{-# INLINE scanl #-}
scanlFB :: (b -> a -> b) -> b -> [a] -> Wrap f r -> (b -> r -> r) -> r -> r
scanlFB f z xs = \w c n -> z `c` foldrW (scanlWrap w) (scanlCons c f) (const n) xs z
{-# INLINE scanlFB #-}
scanlWrap :: Wrap f r -> Wrap (Env b f) (b -> r)
scanlWrap (Wrap wrap unwrap) = Wrap
(\(Env s) e k b -> wrap (s b) e (k b))
(\u -> Env $ \b -> unwrap $ \e r -> u e (\b' -> r) b)
{-# INLINE[0] scanlWrap #-}
scanlCons :: (b -> r -> r) -> (b -> a -> b) -> a -> (b -> r) -> b -> r
scanlCons c f = \e k acc -> let acc' = f acc e in c acc' $ k acc'
{-# INLINE[0] scanlCons #-}
{-# RULES
"foldrW/buildW" forall
f z
(i :: Wrap f b)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
foldrW i f z (buildW g) = g i f z
"foldrW/buildWStatic" forall
f z
(i :: Wrap f b)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
foldrW i f z (buildWStatic g) = g i f z
"foldrW/augmentW" forall
f z
(i :: forall e. Wrap (f e) (e -> b -> b))
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
xs
.
foldrW i f z (augmentW g xs) = g i f (foldrW i f z xs)
"foldrW/augmentWStatic" forall
f z
(i :: forall e. Wrap (f e) (e -> b -> b))
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
xs
.
foldrW i f z (augmentWStatic g xs) = g i f (foldrW i f z xs)
"augmentW/buildW" forall
(f :: forall c g.
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
augmentW g (buildW f) = buildW (\i c n -> g i c (f i c n))
"augmentWStatic/buildW" forall
(f :: forall c g.
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
augmentWStatic g (buildW f) = buildW (\i c n -> g i c (f i c n))
"augmentW/buildWStatic" forall
(f :: forall c g.
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
augmentW g (buildWStatic f) = buildW (\i c n -> g i c (f i c n))
"augmentWStatic/buildWStatic" forall
(f :: forall c g.
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
(g :: forall c g .
(Wrap g c)
-> (a -> c -> c)
-> c
-> c)
.
augmentWStatic g (buildWStatic f) = buildWStatic (\i c n -> g i c (f i c n))
#-}