-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.hs
405 lines (321 loc) · 13.1 KB
/
Parser.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
module Parser where
import System.Environment
import System.FilePath.Posix
import Control.Applicative((<*))
import Data.List as L
import Data.Foldable as F
import Data.Tree as T
import Data.Maybe (mapMaybe)
import Data.Map (Map, empty, map, insert, unionWith, unionsWith, singleton,(!),toList)
-- Parser
import Text.ParserCombinators.Parsec
import qualified Text.ParserCombinators.Parsec.Token as T
import Text.ParserCombinators.Parsec.Language (emptyDef)
-- Pretty Printing
import qualified Text.PrettyPrint as PP
import Text.PrettyPrint (Doc, (<>), (<+>))
type Environment = Map String (Int, LocalType)
data Direction = Send | Receive
deriving (Eq, Ord, Read)
instance Show Direction where
show Send = "!"
show Receive = "?"
data LocalType = Act Direction String LocalType -- Send/Receive prefix
| Rec String LocalType -- Recursive def
| Var String -- Recursive call
| End -- End
| Choice Direction [LocalType]
deriving (Eq, Ord, Read)
-- instance Ord LocalType where
-- compare t1 t2 = compare (numberOfTransitions t1) (numberOfTransitions t2)
instance Show LocalType where
show = printLocalType
dual :: Direction -> Direction
dual Send = Receive
dual Receive = Send
dualType :: LocalType -> LocalType
dualType (Act dir s t) = (Act (dual dir) s (dualType t))
dualType (Choice dir xs) = (Choice dir $ L.map dualType xs)
dualType (Rec s t) = Rec s (dualType t)
dualType (Var s) = (Var s)
dualType End = End
getPairs :: [LocalType] -> [(String, LocalType)]
getPairs ((Act Send s lt):xs) = (string2send s, lt):(getPairs xs)
getPairs ((Act Receive s lt):xs) = (string2receive s, lt):(getPairs xs)
getPairs [] = []
getPairs lt = error $ "Ill-formed type: " ++(show lt)
printLabel :: String -> String
printLabel s = if (take 4 s) == "Send"
then drop 4 s
else drop 7 s
string2send :: String -> String
string2send s = "Send"++s
string2receive :: String -> String
string2receive s = "Recv"++s
string2parent :: (String -> String) -> String -> String
string2parent f s = act2parent $ f s
act2parent :: String -> String
act2parent s = ("Parent"++s)
parent2act :: String -> String
parent2act s = drop 6 s
allActions :: LocalType -> ([String],[String])
allActions (Act Send s lt) = let (snd, rcv) = allActions lt
in ((string2send s):snd, rcv)
allActions (Act Receive s lt) = let (snd, rcv) = allActions lt
in (snd, (string2receive s):rcv)
allActions (Rec var lt) = allActions lt
allActions (Var _) = ([],[])
allActions End = ([],[])
allActions (Choice _ xs) = L.foldr (\(s1,r1) -> \(s2,r2) -> (s1++s2,r1++r2)) ([],[]) (L.map allActions xs)
freeVariables :: LocalType -> [String]
freeVariables (Act dir s t) = freeVariables t
freeVariables (Choice dir xs) = nub $ L.foldr (++) [] $ L.map freeVariables xs
freeVariables (Var s) = [s]
freeVariables End = []
freeVariables (Rec s t) = delete s $ freeVariables t
getNextSnd :: [LocalType] -> [LocalType]
getNextSnd xs = L.map (\(Act Send s lt) -> lt) xs
isSend :: LocalType -> Bool
isSend (Act Send s lt) = True
isSend _ = False
isReceive :: LocalType -> Bool
isReceive (Act Receive _ lt) = True
isReceive _ = False
disjointPrefix :: [LocalType] -> Bool
disjointPrefix list = let l = helper list in (length l) == (length $ nub l)
where helper ((Act _ s _):xs) = s:(helper xs)
helper _ = []
isExtChoice :: [LocalType] -> Bool
isExtChoice [] = False
isExtChoice (x:xs) = isReceive x
isIntChoice :: [LocalType] -> Bool
isIntChoice [] = False
isIntChoice (x:xs) = isSend x
wellFormed :: LocalType -> Bool
wellFormed = wellFormed_ []
where wellFormed_ vars (Rec s lt) = (s `L.notElem` vars) && (wellFormed_ (s:vars) lt)
wellFormed_ vars (Var s) = L.elem s vars
wellFormed_ vars (Act Send _ lt) = wellFormed_ vars lt
wellFormed_ vars (Act Receive _ lt) = wellFormed_ vars lt
wellFormed_ _ End = True
wellFormed_ vars (Choice Send list) = (F.and $ L.map isSend list)
&& (disjointPrefix list)
&& (F.and $ L.map (wellFormed_ vars) list)
wellFormed_ vars (Choice Receive list) = (F.and $ L.map isReceive list)
&& (disjointPrefix list)
&& (F.and $ L.map (wellFormed_ vars) list)
typeDepth :: LocalType -> Int
typeDepth (Act dir s t) = 1+(typeDepth t)
typeDepth (Choice dir xs) = let l = L.map typeDepth xs
in if L.null l then 0
else L.maximum l
typeDepth (Var s) = 1
typeDepth End = 1
typeDepth (Rec s t) = 1+(typeDepth t)
numberOfLeaves :: LocalType -> Int
numberOfLeaves End = 1
numberOfLeaves (Var _) = 1
numberOfLeaves (Rec _ t) = (numberOfLeaves t)
numberOfLeaves (Choice dir xs) = L.foldr (+) 0 $ L.map numberOfLeaves xs
numberOfLeaves (Act dir s t) = (numberOfLeaves t)
numberOfTransitions :: LocalType -> (Int, Int)
numberOfTransitions (Act dir s t) =
let (s,r) = numberOfTransitions t
in if dir==Send
then (1+s, r)
else (s, 1+r)
numberOfTransitions (Choice dir xs) = L.foldr (\(x,x') (y,y') -> (x+y,x'+y')) (0,0) $ L.map numberOfTransitions xs
numberOfTransitions (Var s) = (0,0)
numberOfTransitions End = (0,0)
numberOfTransitions (Rec s t) = numberOfTransitions t
average xs = realToFrac (L.sum xs) / genericLength xs
-- (printf "%.4f" (averageDegree ts))
branchingFactor :: LocalType -> Float
branchingFactor t = case helper t of
Just x -> x
Nothing -> 0
where
helper :: LocalType -> Maybe Float
helper (Act dir s t) = helper t
helper (Rec s t) = helper t
helper (Choice dir xs) =
Just $ average $
(fromIntegral $ length xs):((mapMaybe helper xs))
helper (Var _) = Nothing
helper End = Nothing
numberOfRecursions :: LocalType -> Int
numberOfRecursions (Act dir s t) = (numberOfRecursions t)
numberOfRecursions (Choice dir xs) = let l = L.map numberOfRecursions xs
in if L.null l then 0
else L.maximum l
-- L.foldr (+) 0 $ L.map numberOfRecursions xs
numberOfRecursions (Var s) = 0
numberOfRecursions End = 0
numberOfRecursions (Rec s t) = (if s `L.elem` (freeVariables t)
then 1
else 0)
+ (numberOfRecursions t)
firstRecursion :: LocalType -> Int
firstRecursion (Act dir s t) = 1+(firstRecursion t)
firstRecursion (Choice dir xs) = let l = L.map firstRecursion xs
in if L.null l then 0
else L.minimum l
firstRecursion (Var s) = 0
firstRecursion End = 0
firstRecursion (Rec s t) = 0
maxRecursion :: LocalType -> (String, Int)
maxRecursion t = let fmax (_,i) (_,j) = compare i j
l = Data.Map.toList $ recursionDistance t Data.Map.empty
in if L.null l then ("Nothing", 0)
else L.maximumBy fmax l
recursionDistance :: LocalType -> Map String Int -> Map String Int
recursionDistance (Act dir s t) m = recursionDistance t (Data.Map.map (1+) m)
recursionDistance (Choice dir xs) m = unionsWith (\x y -> L.maximum [x,y]) $ L.map (flip recursionDistance m) xs
recursionDistance (Var s) m = Data.Map.singleton s (m!s)
recursionDistance End m = Data.Map.empty
recursionDistance (Rec s t) m = recursionDistance t (Data.Map.insert s 0 m)
varOccurences :: String -> LocalType -> Int
varOccurences s End = 0
varOccurences v (Act dir s t) = varOccurences v t
varOccurences v (Choice dir xs) = L.foldr (+) 0 (L.map (varOccurences v) xs)
varOccurences v (Rec s t) = if s/=v then varOccurences v t
else 0
varOccurences v (Var s) = if s == v then 1
else 0
sizeUnfolded :: [String] -> LocalType -> Int
sizeUnfolded vars End = 0
sizeUnfolded vars (Var s) = 0
sizeUnfolded vars (Act dir s t) = 1+(sizeUnfolded vars t)
sizeUnfolded vars (Choice dir xs) = L.foldr (+) 0 (L.map (sizeUnfolded vars) xs)
sizeUnfolded vars r@(Rec s t) = (1 + (varOccurences s t)) * (sizeUnfolded (s:vars) t)
-- "var" to be replaced by "t" in "(...)"
substitute :: String -> LocalType -> LocalType -> LocalType
substitute var t p = substituteList [var] t p
substituteList :: [String] -> LocalType -> LocalType -> LocalType
substituteList [] t p = p
substituteList var t End = End
substituteList var t (Var v) = if v `L.elem` var then t
else (Var v)
substituteList var t (Rec s t') =
if s `L.elem` var
then Rec s (substituteList var t t')
else Rec s (substituteList (delete s var) t t')
substituteList var t (Choice dir xs) = Choice dir (L.map (substituteList var t) xs)
substituteList var t (Act dir s t') = Act dir s (substituteList var t t')
-- Lexer & Parser
lexer :: T.TokenParser ()
lexer = T.makeTokenParser languageDef
languageDef =
emptyDef { T.commentStart = "/*"
, T.commentEnd = "*/"
, T.commentLine = "--"
, T.identStart = alphaNum
, T.identLetter = alphaNum
, T.reservedNames = []
, T.reservedOpNames = ["!", "?", "+", "&"]
, T.caseSensitive = True
}
whiteSpace= T.whiteSpace lexer
lexeme = T.lexeme lexer
symbol = T.symbol lexer
parens = T.parens lexer
identifier= T.identifier lexer
ltparser :: Parser LocalType
ltparser = do { symbol "!"
; act <- identifier
; symbol ";"
; cont <- ltparser
; return $ Act Send act cont
}
<|>
do { symbol "?"
; act <- identifier
; symbol ";"
; cont <- ltparser
; return $ Act Receive act cont
}
<|>
do { symbol "rec"
; var <- identifier
; symbol "."
; cont <- ltparser
; return $ Rec var cont
}
<|>
do { symbol "end"
; return End
}
<|>
do { var <- identifier
; return $ Var var
}
<|> -- REDUNDANCY TO MAKE IT EASIER TO TYPE TYPES
do { symbol "{"
; list <- sepBy1 ltparser (char ',' <* spaces)
; symbol "}"
; return $ Choice (if (isExtChoice list) then Receive else Send) list
}
<|>
do { symbol "["
; list <- sepBy1 ltparser (char ',' <* spaces)
; symbol "]"
; return $ Choice (if (isIntChoice list) then Send else Receive) list
}
<|>
do { dir <- (symbol "+" <|> symbol "&")
; choice <- choiceParser
; return choice
}
choiceParser =
do { symbol "["
; list <- sepBy1 ltparser (char ',' <* spaces)
; symbol "]"
; return $ Choice (if (isIntChoice list) then Send else Receive) list
}
<|>
do { symbol "{"
; list <- sepBy1 ltparser (char ',' <* spaces)
; symbol "}"
; return $ Choice (if (isIntChoice list) then Send else Receive) list
}
mainparser :: Parser LocalType
mainparser = whiteSpace >> ltparser <* eof
parseLocalType :: String -> Either ParseError LocalType
parseLocalType inp = parse mainparser "" inp
printDirection :: Direction -> String
printDirection Send = "!"
printDirection Receive = "?"
printLocalType :: LocalType -> String
printLocalType (Act dir s lt) = (printDirection dir)++s++"; "++(printLocalType lt)
printLocalType (Rec s lt) = "rec "++s++" . "++(printLocalType lt)
printLocalType End = "end"
printLocalType (Var s) = s
printLocalType (Choice dir xs) = (if dir == Send
then "+{"
else "&[")
++
helper xs
++
(if dir == Send
then "}"
else "]")
where helper (x:y:xs) = (printLocalType x)
++", "++(helper (y:xs))
helper [x] = printLocalType x
helper [] = []
localTypeToTree :: LocalType -> Tree String
localTypeToTree (Act dir s lt) = Node ((printDirection dir)++ s) [localTypeToTree lt]
localTypeToTree (Rec s lt) = Node ("Rec "++s) [localTypeToTree lt]
localTypeToTree End = Node "end" []
localTypeToTree (Var s) = Node s []
localTypeToTree (Choice dir xs) = Node ((printDirection dir)++" choice") (L.map localTypeToTree xs)
printTypeTree :: LocalType -> String
printTypeTree f = drawTree $ localTypeToTree f
reduce :: LocalType -> LocalType
reduce t = helper Nothing t
where helper b (Rec s t) = case b of
Just x -> helper b (substitute s (Var x) t)
Nothing -> Rec s (helper (Just s) t)
helper b (Choice dir xs) = Choice dir (L.map (helper b ) xs)
helper b (Act dir s t) = Act dir s (helper Nothing t)
helper b t = t