-
Notifications
You must be signed in to change notification settings - Fork 4
/
WWFusion.hs
166 lines (147 loc) · 3.3 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
{-# LANGUAGE RankNTypes, ExistentialQuantification #-}
module WWFusion
( foldrW
, buildW
, foldl
, foldl'
, foldr
, filter
, map
, eft
, (++)
, concat
, Wrap(..)
) where
import Prelude hiding ((++), foldl, foldr, concat, filter, map)
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
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f z = foldrW isoSimple f z
{-# INLINE foldr #-}
buildW
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
buildW g = g isoSimple (:) []
{-# INLINE[0] buildW #-}
augmentW
:: (forall b f . (Wrap f b)
-> (a -> b -> b)
-> b
-> b)
-> [a]
-> [a]
augmentW g xs = g isoSimple (:) xs
{-# INLINE[0] augmentW #-}
(++) :: [a] -> [a] -> [a]
a ++ b = augmentW (\i c n -> foldrW i c n a) b
{-# INLINE (++) #-}
concat :: [[a]] -> [a]
concat xs = buildW (\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 (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 #-}
map :: (a -> b) -> [a] -> [b]
map f = \xs -> buildW (mapFB f xs)
mapFB
:: (a -> b)
-> [a]
-> Wrap f r
-> (b -> r -> r)
-> r
-> r
mapFB f xs ww cons nil = foldrW ww (cons . f) nil xs
filter :: (a -> Bool) -> [a] -> [a]
filter p = \xs -> buildW (filterFB p xs)
{-# INLINE filter #-}
eft :: Int -> Int -> [Int]
eft = \from to -> buildW (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 #-}
{-# 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/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)
"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))
#-}