-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.hs
180 lines (119 loc) · 4.71 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
{-# LANGUAGE TupleSections, PatternGuards #-}
module Utilities (
module IdSupply,
module Utilities,
module Control.Arrow,
module Control.Exception,
module Control.Monad,
module Debug.Trace,
module Text.PrettyPrint.HughesPJClass
) where
import IdSupply
import Control.Arrow (first, second, (***), (&&&))
import Control.Exception (assert)
import Control.Monad
import qualified Data.Set as S
import Debug.Trace
import Text.PrettyPrint.HughesPJClass hiding (render)
import System.IO.Unsafe (unsafePerformIO)
instance Pretty a => Pretty (S.Set a) where
pPrint xs = char '{' <> hsep (punctuate (char ',') (map pPrint $ S.toList xs)) <> char '}'
{-# NOINLINE reduceIdSupply #-}
reduceIdSupply :: IdSupply
reduceIdSupply = unsafePerformIO $ initIdSupply 'u'
{-# NOINLINE normaliseIdSupply #-}
normaliseIdSupply :: IdSupply
normaliseIdSupply = unsafePerformIO $ initIdSupply 'n'
stepIdSupply :: IdSupply -> (IdSupply, Id)
stepIdSupply = second idFromSupply . splitIdSupply
data Train a b = Wagon a (Train a b)
| Caboose b
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 = char '<' <> d <> char '>'
coangles d = char '>' <> d <> 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
traceRender :: Pretty a => a -> b -> b
traceRender x = trace (pPrintRender x)
removeOnes :: [a] -> [[a]]
removeOnes [] = []
removeOnes (x:xs) = xs : map (x:) (removeOnes xs)
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
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
splitAtRev :: Int -> [a] -> ([a], [a])
splitAtRev n xs = case splitAt n (reverse xs) of (xs1, xs2) -> (reverse xs2, reverse xs1)
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
fixpoint :: Eq a => (a -> a) -> a -> a
fixpoint f x
| x' == x = x
| otherwise = fixpoint f x'
where x' = f x