-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
306 lines (253 loc) · 10.4 KB
/
Main.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
{-# LANGUAGE OverloadedStrings, BangPatterns #-}
module Main where
import qualified System.Environment as Env
import System.IO (IOMode(..), withFile)
import Text.Printf (hPrintf)
import Text.Read (readMaybe)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State
import Control.Applicative ((<|>))
import Data.Foldable (traverse_)
import Data.Word (Word8)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as C
import Data.Array.Unboxed (UArray, (!))
import qualified Data.Array.IArray as A
import Data.Maybe (catMaybes)
import Data.List (transpose, tails, foldl')
import Dequeue (Dequeue(..), BankersDequeue)
data Formula
= Globally Int Formula
| Eventually Int Formula
| Until Int Formula Formula
| Negation Formula
| Disjunction Formula Formula
| Conjunction Formula Formula
| GreaterThan Int Arithmetic
| LessThan Int Arithmetic
deriving Show
data Arithmetic
= SignalReference Int
| Addition Arithmetic Arithmetic
| Subtraction Arithmetic Arithmetic
| Multiplication Arithmetic Arithmetic
| Mean Int Arithmetic
| Variance Int Arithmetic
deriving Show
type Parser a =
StateT C.ByteString Maybe a
run :: Parser a -> C.ByteString -> Maybe a
run =
evalStateT
parseInt' :: Parser Int
parseInt' = StateT C.readInt
parseChar' :: Char -> Parser ()
parseChar' c = StateT readChar
where
readChar = (matchChar =<<) . C.uncons
matchChar (h, r) = if h == c then Just ((), r) else Nothing
ignoreSpace :: Parser ()
ignoreSpace = parseChar' ' ' <|> return ()
parseInt :: Parser Int
parseInt = ignoreSpace *> parseInt'
parseChar :: Char -> Parser ()
parseChar = (ignoreSpace *>) . parseChar'
parseSignalReference :: Parser Arithmetic
parseSignalReference = SignalReference <$> (parseChar 'x' *> parseInt)
parseAddition :: Parser Arithmetic
parseAddition = Addition <$> (parseChar '+' *> parseArithmetic) <*> parseArithmetic
parseSubtraction :: Parser Arithmetic
parseSubtraction = Subtraction <$> (parseChar '-' *> parseArithmetic) <*> parseArithmetic
parseMultiplication :: Parser Arithmetic
parseMultiplication = Multiplication <$> (parseChar '*' *> parseArithmetic) <*> parseArithmetic
parseMean :: Parser Arithmetic
parseMean = Mean <$> (parseChar 'M' *> parseInt) <*> parseArithmetic
parseVariance :: Parser Arithmetic
parseVariance = Variance <$> (parseChar 'V' *> parseInt) <*> parseArithmetic
parseArithmetic :: Parser Arithmetic
parseArithmetic
= parseSignalReference
<|> parseAddition
<|> parseSubtraction
<|> parseMultiplication
<|> parseMean
<|> parseVariance
parseGlobally :: Parser Formula
parseGlobally = Globally <$> (parseChar 'G' *> parseInt) <*> parseFormula
parseEventually :: Parser Formula
parseEventually = Eventually <$> (parseChar 'F' *> parseInt) <*> parseFormula
parseUntil :: Parser Formula
parseUntil = Until <$> (parseChar 'U' *> parseInt) <*> parseFormula <*> parseFormula
parseNegation :: Parser Formula
parseNegation = Negation <$> (parseChar '!' *> parseFormula)
parseDisjunction :: Parser Formula
parseDisjunction = Disjunction <$> (parseChar '|' *> parseFormula) <*> parseFormula
parseConjunction :: Parser Formula
parseConjunction = Conjunction <$> (parseChar '&' *> parseFormula) <*> parseFormula
parseGreaterThan :: Parser Formula
parseGreaterThan = GreaterThan <$> (parseChar '>' *> parseInt) <*> parseArithmetic
parseLessThan :: Parser Formula
parseLessThan = LessThan <$> (parseChar '<' *> parseInt) <*> parseArithmetic
parseFormula :: Parser Formula
parseFormula
= parseGlobally
<|> parseEventually
<|> parseUntil
<|> parseNegation
<|> parseDisjunction
<|> parseConjunction
<|> parseGreaterThan
<|> parseLessThan
-- END PARSER
type Signal = [Double]
gfst :: (a, b, c) -> a
gfst (!a, _, _) = a
clearLastElementsFromQueue :: (Double -> Double -> Bool) -> BankersDequeue (Int, Double) -> Double -> BankersDequeue (Int, Double)
clearLastElementsFromQueue pred queue e =
case popBack queue of
Nothing -> queue
Just ((!lastInd, !lastVal), !rest) ->
if pred lastVal e
then clearLastElementsFromQueue pred rest e
else queue
streamingMin :: Int -> Signal -> Signal
streamingMin window es = stream es
where
stream = drop window . (map gfst) . (scanl iter (0, 0, empty))
iter :: (Double, Int, BankersDequeue (Int, Double)) -> Double -> (Double, Int, BankersDequeue (Int, Double))
iter (_, !ind, q) !e =
let newQ = pushBack (clearLastElementsFromQueue (>) q e) (ind, e)
in case popFront newQ of
Nothing -> (0, ind + 1, newQ)
Just (!(!firstInd, !firstVal), shiftedQ) ->
let nextQ = if firstInd + window == ind + 1 then shiftedQ else newQ
in (firstVal, ind + 1, nextQ)
streamingMax :: Int -> Signal -> Signal
streamingMax window es = stream es
where
stream = (drop window) . (map gfst) . (scanl iter (0, 0, empty))
iter :: (Double, Int, BankersDequeue (Int, Double)) -> Double -> (Double, Int, BankersDequeue (Int, Double))
iter (_, !ind, !q) !e =
let newQ = e `seq` pushBack (clearLastElementsFromQueue (<) q e) (ind, e)
in case popFront newQ of
Nothing -> (0, ind + 1, newQ)
Just (!(!firstInd, !firstVal), shiftedQ) ->
let nextQ = if firstInd + window == ind + 1 then shiftedQ else newQ
in (firstVal, ind + 1, nextQ)
slidingUntil :: Int -> Signal -> Signal -> Signal
slidingUntil n as bs = stream
where
stream =
fmap
maximum
(zipWith
(zipWith min)
(ev bs)
(gl as)
)
ev :: [Double] -> [[Double]]
ev = takeWhile ((==n) . length) . fmap (take n) . tails
gl :: [Double] -> [[Double]]
gl = fmap (scanl min 99999999) . ev
streamingMean :: Int -> Signal -> Signal
streamingMean window es = stream es
where
w = fromIntegral window
stream = (drop window) . (map gfst) . (scanl iter (0, 0, empty))
iterVal :: Double -> Double -> Double -> Double
iterVal lastMean newItem firstItem = (lastMean * w + newItem - firstItem) / w
iter :: (Double, Int, BankersDequeue (Int, Double)) -> Double -> (Double, Int, BankersDequeue (Int, Double))
iter (lastVal, ind, q) newItem =
case popFront q of
Nothing ->
-- just starting, the queue has no items yet.
( iterVal lastVal newItem 0
, ind + 1
, pushBack q (ind, newItem)
)
Just ((firstInd, firstItem), shiftedQ) ->
if firstInd + window == ind + 1
-- reached window size, so we pop from front of queue
then ( iterVal lastVal newItem firstItem
, ind + 1
, pushBack shiftedQ (ind, newItem)
)
-- not reached window size, so we dont pop
else ( iterVal lastVal newItem 0
, ind + 1
, pushBack q (ind, newItem)
)
streamingVariance :: Int -> Signal -> Signal
streamingVariance window es = stream es
where
w = fromIntegral window
stream = (drop window) . (map (gfst . gfst)) . (scanl iter ((0, 0, 0), 0, empty))
iterVal :: (Double, Double, Double) -> Double -> Double -> (Double, Double, Double)
iterVal (_lastVariance, sumOfSquares, sums) newItem firstItem =
let sumOfSquares' = sumOfSquares + (newItem * newItem) - (firstItem * firstItem)
sums' = sums + newItem - firstItem
in ( (sumOfSquares' * w - (sums' * sums')) / (w * (w - 1))
, sumOfSquares'
, sums'
)
iter :: ((Double, Double, Double), Int, BankersDequeue (Int, Double)) -> Double -> ((Double, Double, Double), Int, BankersDequeue (Int, Double))
iter (lastVal, ind, q) newItem =
case popFront q of
Nothing ->
-- just starting, the queue has no items yet.
( iterVal lastVal newItem 0
, ind + 1
, pushBack q (ind, newItem)
)
Just ((firstInd, firstItem), shiftedQ) ->
if firstInd + window == ind + 1
-- reached window size, so we pop from front of queue
then ( iterVal lastVal newItem firstItem
, ind + 1
, pushBack shiftedQ (ind, newItem)
)
-- not reached window size, so we dont pop
else ( iterVal lastVal newItem 0
, ind + 1
, pushBack q (ind, newItem)
)
type Trace = [[Double]]
evalArithmetic :: Trace -> Arithmetic -> Signal
evalArithmetic trace formula =
case formula of
SignalReference ref -> trace !! ref
Addition f1 f2 -> zipWith (+) (evalArithmetic trace f1) (evalArithmetic trace f2)
Subtraction f1 f2 -> zipWith (-) (evalArithmetic trace f1) (evalArithmetic trace f2)
Multiplication f1 f2 -> zipWith (*) (evalArithmetic trace f1) (evalArithmetic trace f2)
Mean r f -> streamingMean r (evalArithmetic trace f)
Variance r f -> streamingVariance r (evalArithmetic trace f)
evalFormula :: Trace -> Formula -> Signal
evalFormula trace formula =
case formula of
LessThan v f -> fmap (\e -> fromIntegral v - e) (evalArithmetic trace f)
GreaterThan v f -> fmap (\e -> e - fromIntegral v) (evalArithmetic trace f)
Conjunction f1 f2 -> zipWith min (evalFormula trace f1) (evalFormula trace f2)
Disjunction f1 f2 -> zipWith max (evalFormula trace f1) (evalFormula trace f2)
Negation f -> fmap (\e -> 0 - e) (evalFormula trace f)
Until r f1 f2 -> slidingUntil (r + 1) (evalFormula trace f1) (evalFormula trace f2)
Eventually r f -> streamingMax (r + 1) (evalFormula trace f)
Globally r f -> streamingMin (r + 1) (evalFormula trace f)
readSamples :: String -> [[Double]]
readSamples = transpose . fmap readSamplesOnLine . lines
where
readSamplesOnLine = catMaybes . fmap readMaybe . words
entry formulaFile traceFile resultFile = do
putStrLn ("Formula file: " ++ formulaFile)
putStrLn ("Trace file: " ++ traceFile)
putStrLn ("Result file: " ++ resultFile)
contentsOfFormulaFile <- BS.readFile formulaFile
let Just (formula, rest) = runStateT parseFormula contentsOfFormulaFile
putStrLn (show formula)
contentsOfTraceFile <- readFile traceFile
let trace = readSamples contentsOfTraceFile
let result = (evalFormula trace formula)
withFile resultFile WriteMode (\h -> traverse_ (hPrintf h "%.11f\n") result)
main = do
[formulaFile, traceFile, resultFile] <- Env.getArgs
entry formulaFile traceFile resultFile