This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.hs
237 lines (190 loc) · 6.4 KB
/
Stats.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
module Stats(
-- mutable
Stats,
new,
tick,
setPrintStats,
ticks,
theStats,
isEmpty,
null,
Stats.print,
clear,
combine,
-- pure
printStat,
printLStat,
Stat,
Stats.singleton,
Stats.singleStat,
prependStat,
-- monad
MonadStats(..),
StatT,
StatM,
mtick,
mtick',
mticks,
runStatT,
runStatIO,
runStatM,
-- combined
tickStat,
readStat
) where
import Util.Std
import Control.Monad.Reader
import Control.Monad.Writer.Strict
import Data.IORef
import Data.Tree
import Prelude hiding(null)
import System.IO.Unsafe
import qualified Data.Map as Map
import qualified Prelude(null)
import StringTable.Atom
import qualified Doc.Chars as C
import qualified Util.IntBag as IB
splitUp :: Int -> String -> [String]
splitUp n str = filter (not . Prelude.null) (f n str) where
f 0 str = []
f n str = case span (`notElem` "/.{") str of
(x,"") -> [x]
(x,('/':rs)) -> x:f (n - 1) rs
(x,('.':rs)) -> x:f n rs
(x,('{':rs)) -> case span (/= '}') rs of
(a,'}':b) -> x:a:f n b
(a,"") -> [x,a]
_ -> error "this can't happen"
_ -> error "this can't happen"
print greets stats = do
l <- toList stats
let fs = createForest 0 $ sort [(splitUp (-1) $ fromAtom x,y) | (x,y) <- l]
mapM_ putStrLn $ ( draw . fmap p ) (Node (greets,0) fs) where
p (x,0) = x
p (x,n) = x ++ ": " ++ show n
createForest :: a -> [([String],a)] -> Forest (String,a)
createForest def xs = map f gs where
f [(xs,ys)] = Node (intercalate "." xs,ys) []
f xs@((x:_,_):_) = Node (x,def) (createForest def [ (xs,ys) | (_:xs@(_:_),ys)<- xs])
f _ = error "createForest: should not happen."
gs = groupBy (\(x:_,_) (y:_,_) -> x == y) xs
draw :: Tree String -> [String]
draw (Node x ts0) = x : drawSubTrees ts0
where drawSubTrees [] = []
drawSubTrees [t] =
{-[vLine] :-} shift lastBranch " " (draw t)
drawSubTrees (t:ts) =
{-[vLine] :-} shift branch (C.vLine ++ " ") (draw t) ++ drawSubTrees ts
branch = C.lTee ++ C.hLine
lastBranch = C.llCorner ++ C.hLine
shift first other = zipWith (++) (first : repeat other)
--vLine = chr 0x254F
-- Pure varients
newtype Stat = Stat IB.IntBag
deriving(Eq,Ord,Monoid)
prependStat :: String -> Stat -> Stat
prependStat name (Stat m) = Stat $ IB.fromList [ (fromAtom $ mappend (toAtom $ "{" ++ name ++ "}.") (unsafeIntToAtom x),y) | (x,y) <- IB.toList m ]
printStat greets (Stat s) = do
let fs = createForest 0 $ sort [(splitUp (-1) $ fromAtom (unsafeIntToAtom x),y) | (x,y) <- IB.toList s]
mapM_ putStrLn $ ( draw . fmap p ) (Node (greets,0) fs) where
p (x,0) = x
p (x,n) = x ++ ": " ++ show n
printLStat n greets (Stat s) = do
let fs = createForest 0 $ [ (x,y) | (x,y) <- Map.toList $ Map.fromListWith (+) [( splitUp n (fromAtom (unsafeIntToAtom x)),y) | (x,y) <- IB.toList s]]
mapM_ putStrLn $ ( draw . fmap p ) (Node (greets,0) fs) where
p (x,0) = x
p (x,n) = x ++ ": " ++ show n
--------------
-- monad stats
--------------
class Monad m => MonadStats m where
mticks' :: Int -> Atom -> m ()
mtickStat :: Stat -> m ()
newtype StatT m a = StatT (WriterT Stat m a)
deriving(MonadIO, Functor, MonadFix, MonadTrans, Monad)
runStatT :: Monad m => StatT m a -> m (a,Stat)
runStatT (StatT m) = runWriterT m
data StatM a = StatM a !Stat
instance Functor StatM where
fmap f (StatM a s) = StatM (f a) s
instance Monad StatM where
StatM _ s1 >> StatM y s2 = StatM y (s1 `mappend` s2)
return x = StatM x mempty
StatM x s1 >>= y = case y x of StatM z s2 -> StatM z (s1 `mappend` s2)
instance Stats.MonadStats StatM where
mticks' 0 k = StatM () mempty
mticks' n k = StatM () $ Stats.singleStat n k
mtickStat s = StatM () s
runStatM :: StatM a -> (a,Stat)
runStatM (StatM a s) = (a,s)
-- These are inlined so the 'toAtom' can become a caf and be shared
{-# INLINE mtick #-}
{-# INLINE mticks #-}
mtick k = mticks 1 k
mtick' k = mticks' 1 k
mticks 0 _ = return ()
mticks n k = let k' = toAtom k in k' `seq` n `seq` mticks' n k'
instance MonadStats Identity where
mticks' _ _ = return ()
mtickStat _ = return ()
instance MonadReader r m => MonadReader r (StatT m) where
ask = lift $ ask
local f (StatT m) = StatT $ local f m
instance (Monad m, Monad (t m), MonadTrans t, MonadStats m) => MonadStats (t m) where
mticks' n k = lift $ mticks' n k
mtickStat s = lift $ mtickStat s
instance Monad m => MonadStats (StatT m) where
mticks' n k = StatT $ tell (Stat $ IB.msingleton (fromAtom k) n)
mtickStat s = StatT $ tell s
singleton n = Stat $ IB.singleton (fromAtom $ toAtom n)
singleStat :: ToAtom a => Int -> a -> Stat
singleStat 0 _ = mempty
singleStat n k = Stat $ IB.msingleton (fromAtom $ toAtom k) n
null (Stat r) = IB.null r
instance MonadStats IO where
mticks' 0 _ = return ()
mticks' n a = do
p <- readIORef printStats
when p (putStrLn $ (show a ++ ": " ++ show n))
ticks theStats n a
mtickStat (Stat s) = do
tickStat theStats (Stat s)
p <- readIORef printStats
when p $ forM_ (IB.toList s) $ \ (x,y) -> do
putStrLn (show (unsafeIntToAtom x) ++ ": " ++ show y)
--------------------
-- Stateful IO stats
--------------------
newtype Stats = Stats (IORef Stat)
{-# NOINLINE theStats #-}
theStats :: Stats
theStats = unsafePerformIO new
{-# NOINLINE printStats #-}
printStats :: IORef Bool
printStats = unsafePerformIO $ newIORef False
setPrintStats :: Bool -> IO ()
setPrintStats b = writeIORef printStats b
combine :: Stats -> Stats -> IO ()
combine (Stats s1) (Stats s2) = do
s <- readIORef s2
modifyIORef s1 (mappend s)
new = Stats `liftM` newIORef mempty
clear (Stats h) = writeIORef h mempty
toList (Stats r) = do
Stat s <- readIORef r
return [(unsafeIntToAtom x,y) | (x,y) <- IB.toList s]
isEmpty (Stats r) = null `liftM` readIORef r
tick stats k = ticks stats 1 k
ticks (Stats r) c k = modifyIORef r (mappend $ singleStat c k)
-----------------
-- pure + mutable
-----------------
tickStat :: Stats -> Stat -> IO ()
tickStat (Stats r) s = modifyIORef r (mappend s)
runStatIO :: MonadIO m => Stats -> StatT m a -> m a
runStatIO stats action = do
(a,s) <- runStatT action
liftIO $ tickStat stats s
return a
readStat :: Stats -> IO Stat
readStat (Stats r) = readIORef r