forked from OctopiChalmers/mutagen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazy.hs
More file actions
307 lines (267 loc) · 7.48 KB
/
Copy pathLazy.hs
File metadata and controls
307 lines (267 loc) · 7.48 KB
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
{-# LANGUAGE BangPatterns #-}
-- | Tracking lazy evaluation of expressions.
module Test.Mutagen.Lazy
( -- * Lazy evaluation tracking interface
__evaluated__
-- * Lazy type class
, Lazy (..)
, withLazy
, withLazyIO
)
where
import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Word (Word16, Word32, Word64, Word8)
import System.IO.Unsafe (unsafePerformIO)
import Test.Mutagen.Mutation (Pos)
{-------------------------------------------------------------------------------
-- * Lazy evaluation tracking interface
-------------------------------------------------------------------------------}
-- | Injectable function to mark the evaluation an expression at some position.
__evaluated__ :: Pos -> a -> a
__evaluated__ pos expr =
unsafePerformIO $ do
addEvaluatedPos pos
return expr
{-# INLINE __evaluated__ #-}
-- | Global IORef to store evaluated positions.
posRef :: IORef [Pos]
posRef = unsafePerformIO (newIORef [])
{-# NOINLINE posRef #-}
-- | Add evaluated position to the global IORef.
addEvaluatedPos :: Pos -> IO ()
addEvaluatedPos pos = modifyIORef' posRef (reverse pos :)
-- | Reset the global IORef of evaluated positions.
resetPosRef :: IO ()
resetPosRef = modifyIORef' posRef (const [])
-- | Read the global IORef of evaluated positions.
readPosRef :: IO [Pos]
readPosRef = reverse <$> readIORef posRef
{-------------------------------------------------------------------------------
-- * Lazy type class
-------------------------------------------------------------------------------}
-- | Class for types that can track lazy evaluation of their subexpressions.
class Lazy a where
-- | Wrap an entire value (i.e., at every subexpression) with calls to
-- '__evaluated__' with their corresponding positions.
--
-- This is a convenience function defined as:
--
-- @
-- lazy x = lazyNode [] x
-- @
--
-- And you usually want to define 'lazyNode' instead.
lazy :: a -> a
lazy = lazyNode []
-- | Wrap a value at a given position with calls to '__evaluated__'.
--
-- You can use 'withLazy' to test which subexpressions are evaluated by a
-- given function. For example:
--
-- >>> let a = Right (undefined, Just 42) :: Either Bool (String, Maybe Int)
-- >>> withLazy (\x -> case x of Right (_, Just _) -> True; _ -> False) a
-- ([[],[0],[0,1]],True)
--
-- Which indicates that the function evaluated:
--
-- * [] -> root node (Right)
-- * [0] -> Right's 0th child (the tuple)
-- * [0,1] -> the tuple's 1st child (Just)
--
-- While not evaluating neihter the @undefined@ nor the @42@.
lazyNode :: Pos -> a -> a
{-# MINIMAL lazyNode #-}
-- | Find which subexpressions of an input value does a function evaluate when
-- forcing its result to weak head normal form.
withLazy :: (Lazy a) => (a -> b) -> a -> IO ([Pos], b)
withLazy f a = do
resetPosRef
let !b = f (lazy a)
ps <- readPosRef
return (ps, b)
-- | Like 'withLazy', but for functions that already run on IO.
withLazyIO :: (Lazy a) => (a -> IO b) -> a -> IO ([Pos], b)
withLazyIO f a = do
resetPosRef
!b <- f (lazy a)
ps <- readPosRef
return (ps, b)
-- ** Lazy instances
instance Lazy () where
lazyNode = __evaluated__
instance Lazy Int where
lazyNode = __evaluated__
instance Lazy Double where
lazyNode = __evaluated__
instance Lazy Float where
lazyNode = __evaluated__
instance Lazy Word8 where
lazyNode = __evaluated__
instance Lazy Word16 where
lazyNode = __evaluated__
instance Lazy Word32 where
lazyNode = __evaluated__
instance Lazy Word64 where
lazyNode = __evaluated__
instance Lazy Char where
lazyNode = __evaluated__
instance Lazy Bool where
lazyNode = __evaluated__
instance (Lazy a) => Lazy (Maybe a) where
lazyNode pre Nothing =
__evaluated__
pre
Nothing
lazyNode pre (Just a) =
__evaluated__ pre
$ Just (lazyNode (0 : pre) a)
instance (Lazy a, Lazy b) => Lazy (Either a b) where
lazyNode pre (Left x) =
__evaluated__ pre
$ Left (lazyNode (0 : pre) x)
lazyNode pre (Right x) =
__evaluated__ pre
$ Right (lazyNode (0 : pre) x)
instance (Lazy a) => Lazy [a] where
lazyNode pre [] =
__evaluated__
pre
[]
lazyNode pre (x : xs) =
__evaluated__
pre
(lazyNode (0 : pre) x : lazyNode (1 : pre) xs)
instance (Lazy v) => Lazy (Map k v) where
lazyNode pre m =
snd (Map.mapAccum f 0 m)
where
f c v = (c + 1, __evaluated__ pre (lazyNode (c : pre) v))
-- Tuple instances
instance
(Lazy a, Lazy b)
=> Lazy (a, b)
where
lazyNode pre (a, b) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
)
instance
(Lazy a, Lazy b, Lazy c)
=> Lazy (a, b, c)
where
lazyNode pre (a, b, c) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d)
=> Lazy (a, b, c, d)
where
lazyNode pre (a, b, c, d) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e)
=> Lazy (a, b, c, d, e)
where
lazyNode pre (a, b, c, d, e) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e, Lazy f)
=> Lazy (a, b, c, d, e, f)
where
lazyNode pre (a, b, c, d, e, f) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
, lazyNode (5 : pre) f
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e, Lazy f, Lazy g)
=> Lazy (a, b, c, d, e, f, g)
where
lazyNode pre (a, b, c, d, e, f, g) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
, lazyNode (5 : pre) f
, lazyNode (6 : pre) g
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e, Lazy f, Lazy g, Lazy h)
=> Lazy (a, b, c, d, e, f, g, h)
where
lazyNode pre (a, b, c, d, e, f, g, h) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
, lazyNode (5 : pre) f
, lazyNode (6 : pre) g
, lazyNode (7 : pre) h
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e, Lazy f, Lazy g, Lazy h, Lazy i)
=> Lazy (a, b, c, d, e, f, g, h, i)
where
lazyNode pre (a, b, c, d, e, f, g, h, i) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
, lazyNode (5 : pre) f
, lazyNode (6 : pre) g
, lazyNode (7 : pre) h
, lazyNode (8 : pre) i
)
instance
(Lazy a, Lazy b, Lazy c, Lazy d, Lazy e, Lazy f, Lazy g, Lazy h, Lazy i, Lazy j)
=> Lazy (a, b, c, d, e, f, g, h, i, j)
where
lazyNode pre (a, b, c, d, e, f, g, h, i, j) =
__evaluated__
pre
( lazyNode (0 : pre) a
, lazyNode (1 : pre) b
, lazyNode (2 : pre) c
, lazyNode (3 : pre) d
, lazyNode (4 : pre) e
, lazyNode (5 : pre) f
, lazyNode (6 : pre) g
, lazyNode (7 : pre) h
, lazyNode (8 : pre) i
, lazyNode (9 : pre) j
)