forked from batterseapower/supercompilation-by-evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.hs
340 lines (234 loc) · 9.6 KB
/
Utilities.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{-# LANGUAGE TupleSections, PatternGuards, ExistentialQuantification #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Utilities (
module IdSupply,
module Utilities,
module Control.Arrow,
module Control.DeepSeq,
module Control.Monad,
module Data.Maybe,
module Data.List,
module Debug.Trace,
module Text.PrettyPrint.HughesPJClass
) where
import Prelude hiding ((<>))
import IdSupply
import StaticFlags
import Control.Arrow (first, second, (***), (&&&))
import Control.DeepSeq (NFData(..), rnf)
import Control.Monad
import Data.Maybe
import Data.List hiding (uncons)
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Time.Clock.POSIX (getPOSIXTime)
import Debug.Trace
import Text.PrettyPrint.HughesPJClass hiding (render, int, char, first)
import qualified Text.PrettyPrint.HughesPJClass as Pretty(char)
import System.IO
import System.IO.Unsafe (unsafePerformIO)
instance NFData Id where
rnf i = rnf (hashedId i)
type Tag = Int
injectTag :: Int -> Tag -> Tag
injectTag cls tg = cls * tg
data Tagged a = Tagged { tag :: Tag, tagee :: a }
deriving (Eq, Show)
instance NFData a => NFData (Tagged a) where
rnf (Tagged a b) = rnf a `seq` rnf b
instance Functor Tagged where
fmap f (Tagged tg x) = Tagged tg (f x)
instance Pretty a => Pretty (Tagged a) where
pPrintPrec level prec (Tagged tg x) = braces (pPrint tg) <+> pPrintPrec level prec x
instance Show IdSupply where
show = show . idFromSupply
instance Pretty Doc where
pPrint = id
instance Pretty Id where
pPrint = text . show
instance Pretty a => Pretty (S.Set a) where
pPrint xs = braces $ hsep (punctuate comma (map pPrint $ S.toList xs))
instance (Pretty k, Pretty v) => Pretty (M.Map k v) where
pPrint m = brackets $ fsep (punctuate comma [pPrint k <+> text "|->" <+> pPrint v | (k, v) <- M.toList m])
fmapSet :: (Ord a, Ord b) => (a -> b) -> S.Set a -> S.Set b
fmapSet f = S.fromList . map f . S.toList
fmapMap :: (Ord a, Ord b) => (a -> b) -> M.Map a v -> M.Map b v
fmapMap f = M.fromList . map (first f) . M.toList
restrict :: Ord k => M.Map k v -> S.Set k -> M.Map k v
restrict m s = M.filterWithKey (\k _ -> k `S.member` s) m
exclude :: Ord k => M.Map k v -> S.Set k -> M.Map k v
exclude m s = M.filterWithKey (\k _ -> k `S.notMember` s) m
{-# NOINLINE parseIdSupply #-}
parseIdSupply :: IdSupply
parseIdSupply = unsafePerformIO $ initIdSupply 'a'
{-# NOINLINE reduceIdSupply #-}
reduceIdSupply :: IdSupply
reduceIdSupply = unsafePerformIO $ initIdSupply 'u'
{-# NOINLINE tagIdSupply #-}
tagIdSupply :: IdSupply
tagIdSupply = unsafePerformIO $ initIdSupply 't'
{-# NOINLINE prettyIdSupply #-}
prettyIdSupply :: IdSupply
prettyIdSupply = unsafePerformIO $ initIdSupply 'p'
{-# NOINLINE matchIdSupply #-}
matchIdSupply :: IdSupply
matchIdSupply = unsafePerformIO $ initIdSupply 'm'
stepIdSupply :: IdSupply -> (IdSupply, Id)
stepIdSupply = second idFromSupply . splitIdSupply
appPrec, opPrec, noPrec :: Rational
appPrec = 2 -- Argument of a function application
opPrec = 1 -- Argument of an infix operator
noPrec = 0 -- Others
normalLevel, haskellLevel :: PrettyLevel
normalLevel = PrettyLevel 0
haskellLevel = PrettyLevel 1
angles, coangles :: Doc -> Doc
angles d = Pretty.char '<' <> d <> Pretty.char '>'
coangles d = Pretty.char '>' <> d <> Pretty.char '<'
pPrintPrec' :: Pretty a => a -> PrettyLevel -> Rational -> Doc
pPrintPrec' x level prec = pPrintPrec level prec x
-- NB: this render function is exported instead of the one from the library
render :: Doc -> String
render = renderStyle (style { lineLength = 120 })
pPrintRender :: Pretty a => a -> String
pPrintRender = render . pPrint
panic :: String -> Doc -> a
panic s d = error $ s ++ ": " ++ render d
traceRender :: Pretty a => a -> b -> b
traceRender x | qUIET = id
| otherwise = trace (pPrintRender x)
assertRender :: Pretty a => a -> Bool -> b -> b
assertRender _ _ x | not aSSERTIONS = x
assertRender _ True x = x
assertRender a False _ = error (pPrintRender a)
removeOnes :: [a] -> [[a]]
removeOnes [] = []
removeOnes (x:xs) = xs : map (x:) (removeOnes xs)
accumL :: (acc -> (acc, a)) -> acc -> Int -> (acc, [a])
accumL f = go
where
go acc n | n <= 0 = (acc, [])
| (acc, x) <- f acc = second (x:) (go acc (n - 1))
instance (Pretty a, Pretty b, Pretty c, Pretty d,
Pretty e, Pretty f, Pretty g, Pretty h,
Pretty i) => Pretty (a, b, c, d, e, f, g, h, i) where
pPrint (a, b, c, d, e, f, g, h, i)
= pPrintTuple [pPrint a, pPrint b, pPrint c, pPrint d,
pPrint e, pPrint f, pPrint g, pPrint h,
pPrint i]
instance (Pretty a, Pretty b, Pretty c, Pretty d,
Pretty e, Pretty f, Pretty g, Pretty h,
Pretty i, Pretty j) => Pretty (a, b, c, d, e, f, g, h, i, j) where
pPrint (a, b, c, d, e, f, g, h, i, j)
= pPrintTuple [pPrint a, pPrint b, pPrint c, pPrint d,
pPrint e, pPrint f, pPrint g, pPrint h,
pPrint i, pPrint j]
instance (Pretty a, Pretty b, Pretty c, Pretty d,
Pretty e, Pretty f, Pretty g, Pretty h,
Pretty i, Pretty j, Pretty k) => Pretty (a, b, c, d, e, f, g, h, i, j, k) where
pPrint (a, b, c, d, e, f, g, h, i, j, k)
= pPrintTuple [pPrint a, pPrint b, pPrint c, pPrint d,
pPrint e, pPrint f, pPrint g, pPrint h,
pPrint i, pPrint j, pPrint k]
instance (Pretty a, Pretty b, Pretty c, Pretty d,
Pretty e, Pretty f, Pretty g, Pretty h,
Pretty i, Pretty j, Pretty k, Pretty l,
Pretty m, Pretty n, Pretty o) => Pretty (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) where
pPrint (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
= pPrintTuple [pPrint a, pPrint b, pPrint c, pPrint d,
pPrint e, pPrint f, pPrint g, pPrint h,
pPrint i, pPrint j, pPrint k, pPrint l,
pPrint m, pPrint n, pPrint o]
pPrintTuple :: [Doc] -> Doc
pPrintTuple ds = parens $ fsep $ punctuate comma ds
data SomePretty = forall a. Pretty a => SomePretty a
instance Pretty SomePretty where
pPrintPrec level prec (SomePretty x) = pPrintPrec level prec x
newtype PrettyFunction = PrettyFunction (PrettyLevel -> Rational -> Doc)
instance Pretty PrettyFunction where
pPrintPrec level prec (PrettyFunction f) = f level prec
asPrettyFunction :: Pretty a => a -> PrettyFunction
asPrettyFunction = PrettyFunction . pPrintPrec'
fst3 :: (a, b, c) -> a
fst3 (a, _, _) = a
snd3 :: (a, b, c) -> b
snd3 (_, b, _) = b
thd3 :: (a, b, c) -> c
thd3 (_, _, c) = c
first3 :: (a -> d) -> (a, b, c) -> (d, b, c)
first3 f (a, b, c) = (f a, b, c)
second3 :: (b -> d) -> (a, b, c) -> (a, d, c)
second3 f (a, b, c) = (a, f b, c)
third3 :: (c -> d) -> (a, b, c) -> (a, b, d)
third3 f (a, b, c) = (a, b, f c)
second4 :: (b -> e) -> (a, b, c, d) -> (a, e, c, d)
second4 f (a, b, c, d) = (a, f b, c, d)
third4 :: (c -> e) -> (a, b, c, d) -> (a, b, e, d)
third4 f (a, b, c, d) = (a, b, f c, d)
secondM :: Monad m => (b -> m c) -> (a, b) -> m (a, c)
secondM f (a, b) = liftM (a,) $ f b
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (a, b, c) = f a b c
splitBy :: [b] -> [a] -> ([a], [a])
splitBy [] xs = ([], xs)
splitBy (_:ys) (x:xs) = first (x:) $ splitBy ys xs
splitManyBy :: [[b]] -> [a] -> [[a]]
splitManyBy [] xs = [xs]
splitManyBy (ys:yss) xs = case splitBy ys xs of (xs1, xs2) -> xs1 : splitManyBy yss xs2
takeFirst :: (a -> Bool) -> [a] -> (Maybe a, [a])
takeFirst p = takeFirstJust (\x -> guard (p x) >> return x)
takeFirstJust :: (a -> Maybe b) -> [a] -> (Maybe b, [a])
takeFirstJust _ [] = (Nothing, [])
takeFirstJust p (x:xs)
| Just y <- p x = (Just y, xs)
| otherwise = second (x:) $ takeFirstJust p xs
expectJust :: String -> Maybe a -> a
expectJust _ (Just x) = x
expectJust s Nothing = error $ "expectJust: " ++ s
safeFromLeft :: Either a b -> Maybe a
safeFromLeft (Left x) = Just x
safeFromLeft _ = Nothing
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
expectHead :: String -> [a] -> a
expectHead s = expectJust s . safeHead
uncons :: [a] -> Maybe (a, [a])
uncons [] = Nothing
uncons (x:xs) = Just (x, xs)
fixpoint :: Eq a => (a -> a) -> a -> a
fixpoint f x
| x' == x = x
| otherwise = fixpoint f x'
where x' = f x
zipWithEqualM :: (MonadFail m, Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithEqualM _ [] [] = return []
zipWithEqualM f (x:xs) (y:ys) = liftM2 (:) (f x y) (zipWithEqualM f xs ys)
zipWithEqualM _ _ _ = fail "zipWithEqualM"
zipWithEqualM_ :: (MonadFail m, Monad m) => (a -> b -> m ()) -> [a] -> [b] -> m ()
zipWithEqualM_ _ [] [] = return ()
zipWithEqualM_ f (x:xs) (y:ys) = f x y >> zipWithEqualM_ f xs ys
zipWithEqualM_ _ _ _ = fail "zipWithEqualM_"
zipWithEqual :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]
zipWithEqual _ [] [] = Just []
zipWithEqual f (x:xs) (y:ys) = fmap (f x y :) $ zipWithEqual f xs ys
zipWithEqual _ _ _ = fail "zipWithEqual"
implies :: Bool -> Bool -> Bool
implies cond consq = not cond || consq
seperate :: Eq a => a -> [a] -> [[a]]
seperate c = go []
where
go sofar [] = [reverse sofar]
go sofar (x:xs)
| x == c = reverse sofar : go [] xs
| otherwise = go (x:sofar) xs
type Seconds = Double
time_ :: IO a -> IO Seconds
time_ = fmap fst . time
time :: IO a -> IO (Seconds, a)
time act = do { start <- getTime; res <- act; end <- getTime; return (end - start, res) }
getTime :: IO Seconds
getTime = (fromRational . toRational) `fmap` getPOSIXTime
type Bytes = Integer
fileSize :: FilePath -> IO Bytes
fileSize file = withFile file ReadMode hFileSize