-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathShip.hs
462 lines (373 loc) · 12.2 KB
/
Ship.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
{- |
Module : $Header$
Copyright : (c) C. Maeder DFKI GmbH
License : GPLv2 or higher, see LICENSE.txt
Maintainer : Christian.Maeder@dfki.de
Stability : provisional
Portability : portable
monitor syntax
-}
module ExtModal.Ship where
import OWL2.ShipSyntax
import OWL2.AS
import Common.Doc
import Common.DocUtils
import Common.Parsec
import Control.Monad
import Text.ParserCombinators.Parsec
data PreOp = NotF | X | F | G | QuantF QuantifierType [ABox]
deriving (Show, Eq, Ord)
data BinOp = Until | Impl deriving (Show, Eq, Ord)
data Foltl
= ABoxass ABox
| Call Bool String [String] -- True: run a process, but deprecated
| JoinedF JunctionType [Foltl]
| PreOp PreOp Foltl
| BinOp Foltl BinOp Foltl
deriving (Show, Eq, Ord)
constFoltl :: String -> Foltl
constFoltl s = Call False s []
trueFoltl, falseFoltl :: Foltl
trueFoltl = constFoltl "true"
falseFoltl = constFoltl "false"
negFoltl :: Foltl -> Foltl
negFoltl ftl = let nftl = PreOp NotF ftl in case ftl of
ABoxass _ -> nftl
Call {} | trueFoltl == ftl -> falseFoltl
| falseFoltl == ftl -> trueFoltl
| otherwise -> nftl
JoinedF t fs -> JoinedF (case t of
UnionOf -> IntersectionOf
IntersectionOf -> UnionOf) $ map negFoltl fs
PreOp p f -> (case p of
QuantF q as -> PreOp (QuantF (case q of
AllValuesFrom -> SomeValuesFrom
SomeValuesFrom -> AllValuesFrom) as)
X -> PreOp X
F -> PreOp G
G -> PreOp F
NotF -> id) $ negFoltl f
BinOp f1 p f2 -> case p of
Until -> release (negFoltl f1) $ negFoltl f2
Impl -> JoinedF IntersectionOf [negFoltl f1, negFoltl f2]
-- <http://en.wikipedia.org/wiki/Linear_temporal_logic>
weakUntil :: Foltl -> Foltl -> Foltl
weakUntil f1 f2 = JoinedF UnionOf [BinOp f1 Until f2, PreOp G f1]
release :: Foltl -> Foltl -> Foltl
release f1 f2 = weakUntil f2 $ JoinedF IntersectionOf [f1, f2]
data Header = Header String [String]
deriving (Show, Eq, Ord)
data Monitor = Monitor Header (Maybe String) Foltl
deriving (Show, Eq, Ord)
type ABoxLit = (Bool, ABox)
type ABoxDeletes = [Either ABoxLit [String]]
type ABoxs = [ABox]
type ABoxAnds = [ABoxLit]
data CondEffect = CondEffect ABox ABoxDeletes
deriving (Show, Eq, Ord)
data Exec = Exec String String [String]
deriving (Show, Eq, Ord)
data Action = Action Header ABoxs ABoxDeletes [CondEffect] (Maybe Exec)
deriving (Show, Eq, Ord)
data IndEffect = IndEffect String ABoxs ABoxDeletes ABoxs
deriving (Show, Eq, Ord)
data Process = Process Header Proc
deriving (Show, Eq, Ord)
data Proc
= While ABoxAnds Proc
| Star Proc
| Quest ABox
| IfElse ABoxAnds Proc Proc
| Switch [(Maybe ABoxAnds, Proc)]
| Forall ABoxAnds Proc
| Init Foltl
| CallP String [String]
| BinP BinP [Proc]
deriving (Show, Eq, Ord)
data BinP = Semi | Pipe | SeqPlus deriving (Show, Eq, Ord)
data Ship
= ShipP Process
| ShipA Action
| ShipI IndEffect
deriving (Show, Eq, Ord)
isPrimFoltl :: Foltl -> Bool
isPrimFoltl ftl = case ftl of
ABoxass _ -> True
Call {} -> True
PreOp _ f -> isPrimFoltl f
_ -> False
lastNotPrim :: [Foltl] -> Bool
lastNotPrim l = not (null l) && not (isPrimFoltl $ last l)
ppJFoltl :: Bool -> JunctionType -> Foltl -> Doc
ppJFoltl first j f = case f of
JoinedF t l -> (if t /= j && t == UnionOf
|| first && lastNotPrim l then parens else id) $ ppFoltl f
_ -> ppBFoltl first f
ppBFoltl :: Bool -> Foltl -> Doc
ppBFoltl first f = case f of
BinOp {} -> parens
JoinedF _ l | first && lastNotPrim l
-> parens
PreOp {} | first -> parens
_ -> id
$ ppFoltl f
ppFoltl :: Foltl -> Doc
ppFoltl ft = case ft of
ABoxass a -> ppABox a
Call b s ps -> (if b then keyword "run" else empty)
<+> text s <> if null ps then empty else ppParams ps
JoinedF t l -> case reverse l of
[] -> empty
f : r -> fsep . prepPunctuate (text $ case t of
UnionOf -> "or "
IntersectionOf -> "and ")
. reverse $ ppJFoltl False t f : map (ppJFoltl True t) r
PreOp p f -> let
d1 = ppPreOp p
d2 = ppFoltl f
d3 = d1 <+> d2
in case p of
QuantF {} -> fsep [d1, bullet <+> d2]
NotF -> case f of
PreOp {} -> d3
_ -> if isPrimFoltl f then d3 else d1 <> parens d2
_ -> d3
BinOp f1 o f2 -> fsep
[ ppBFoltl True f1
, case o of
Until -> keyword "U"
Impl -> implies
, ppBFoltl False f2 ]
ppPreOp :: PreOp -> Doc
ppPreOp o = case o of
QuantF q as -> keyword (showQuant q) <+> sepByCommas (map ppABox as)
NotF -> keyword "not"
_ -> keyword (show o)
ppHeader :: Header -> Doc
ppHeader (Header name ps) = text name <>
(if null ps then empty else ppParams ps)
<+> equals
ppMonitor :: Monitor -> Doc
ppMonitor (Monitor h mc ft) = fsep
$ (keyword "monitor" <+> ppHeader h)
: case mc of
Nothing -> []
Just c -> [doubleQuotes . text $ filter (/= '"') c]
++ [ppFoltl ft]
ppMon :: [Monitor] -> Doc
ppMon = vsep . map ppMonitor
ppABoxLit :: ABoxLit -> Doc
ppABoxLit (b, a) = (if b then empty else keyword "not") <+> ppABox a
ppABoxDelete :: Either ABoxLit [String] -> Doc
ppABoxDelete e = case e of
Left a -> ppABoxLit a
Right l -> keyword "delete" <> ppParams l
ppABoxDeletes :: ABoxDeletes -> Doc
ppABoxDeletes = sepByCommas . map ppABoxDelete
ppABoxs :: ABoxs -> Doc
ppABoxs = sepByCommas . map ppABox
ppABoxAnds :: ABoxAnds -> Doc
ppABoxAnds = fsep . prepPunctuate (text "and ") . map ppABoxLit
ppCondEffect :: CondEffect -> Doc
ppCondEffect (CondEffect a as) =
fsep [keyword "if" <+> parens (ppABox a), ppABoxDeletes as]
eqKey :: String -> Doc
eqKey s = keyword s <+> equals
ppParams :: [String] -> Doc
ppParams = parens . sepByCommas . map text
ppExec :: Exec -> [Doc]
ppExec (Exec s1 s2 ps) =
[eqKey "exec" <+> text s1 <> dot <> text s2 <> ppParams ps]
ppAction :: Action -> Doc
ppAction (Action h pre eff cs me) = fsep
[ keyword "action" <+> ppHeader h
, braces $ vcat
$ [ if null pre then empty else eqKey "pre" <+> ppABoxs pre
, if null eff then empty else eqKey "effect" <+> ppABoxDeletes eff]
++ map ppCondEffect cs
++ maybe [] ppExec me]
ppIndEffect :: IndEffect -> Doc
ppIndEffect (IndEffect n is cs ds) = fsep
[ keyword "indirect" <+> keyword "effect" <+> ppHeader (Header n [])
, braces $ vcat
[ eqKey "init" <+> ppABoxs is
, eqKey "causes" <+> ppABoxDeletes cs
, eqKey "cond" <+> ppABoxs ds]]
ppProcess :: Process -> Doc
ppProcess (Process h p) = fsep
[ keyword "function" <+> ppHeader h, braces $ ppProc p]
ppProc :: Proc -> Doc
ppProc pr = case pr of
While as p -> fsep
[keyword "while" <+> parens (ppABoxAnds as), ppProc p]
Star p -> (if isPrim p then id else parens) (ppProc p) <> text "*"
Quest a -> ppABox a <> text "?"
IfElse as p1 p2 -> fsep
[ keyword "if" <+> parens (ppABoxAnds as), ppProc p1
, keyword "else" <+> ppProc p2]
Switch cs -> keyword "switch" <+>
vcat (map (\ (m, p) -> fsep
[keyword "case" <+> maybe (text "_") ppABoxAnds m, implies <+> ppProc p])
cs)
Forall as p -> fsep
[keyword "forall" <+> ppABoxAnds as, implies <+> ppProc p]
Init f -> keyword "init" <+> ppFoltl f
CallP n ps -> text n <> ppParams ps
BinP o ps -> case reverse ps of
[] -> empty
f : r -> fsep . prepPunctuate (ppBinP o)
. reverse $ ppBinProc False o f : map (ppBinProc True o) r
isPrim :: Proc -> Bool
isPrim p = case p of
Star _ -> True
Quest _ -> True
CallP {} -> True
Init {} -> True
_ -> False
lastIsPrim :: Proc -> Bool
lastIsPrim pr = case pr of
While _ p -> lastIsPrim p
IfElse _ _ p -> lastIsPrim p
Forall _ p -> lastIsPrim p
Switch l -> null l || lastIsPrim (snd $ last l)
_ -> isPrim pr
ppBinProc :: Bool -> BinP -> Proc -> Doc
ppBinProc first b p = case p of
BinP o ps | o <= b -> if first && not (null ps || isPrim (last ps))
then parens else id
_ | first -> if isPrim p then id else parens
_ | lastIsPrim p -> id
_ -> parens
$ ppProc p
ppBinP :: BinP -> Doc
ppBinP b = text $ case b of
Semi -> "; "
Pipe -> "| "
SeqPlus -> "+> "
ppShip :: Ship -> Doc
ppShip s = case s of
ShipP p -> ppProcess p
ShipA a -> ppAction a
ShipI i -> ppIndEffect i
ppShips :: [Ship] -> Doc
ppShips = vsep . map ppShip
aboxVar :: CharParser st [ABox]
aboxVar = liftM2 (\ ns c -> map (`AConcept` c) ns)
(sepBy nominal commaP)
(colonP >> concept)
aboxVars :: CharParser st [ABox]
aboxVars = flat $ sepBy aboxVar commaP
foltl, primFoltl, preFoltl, quantFoltl, andFoltl, orFoltl :: CharParser st Foltl
primFoltl = fmap ABoxass (try abox)
<|> fmap (PreOp NotF) (skipKey "not" >> quantFoltl)
<|> parent foltl
preFoltl = liftM2 PreOp
(choice $ map (\ p -> skipKey (show p) >> return p) [X, F, G])
foltl
<|> primFoltl
quantFoltl = do
q <- quant << skip
as <- aboxVars
bulletP
f <- foltl
return $ PreOp (QuantF q as) f
<|> preFoltl
<|> (option False (skipKey "run" >> return True) >>= callFoltl)
andFoltl = binP (JoinedF IntersectionOf) "and" quantFoltl
orFoltl = binP (JoinedF UnionOf) "or" andFoltl
impliesP :: GenParser Char st ()
impliesP = tryString "=>" >> skip
foltl = do
f <- orFoltl
option f $ liftM2 (BinOp f)
((skipKey "U" >> return Until) <|> (impliesP >> return Impl))
foltl
optNoms :: GenParser Char st [String]
optNoms = optionL . parent $ sepBy nominal commaP
callFoltl :: Bool -> GenParser Char st Foltl
callFoltl b = liftM2 (Call b)
(reserved (map show [X, F, G]
++ ["run", "monitor", "and", "or", "U"]) nominal)
optNoms
header :: CharParser st Header
header = liftM2 Header nominal optNoms << equalP
monitor :: CharParser st Monitor
monitor = do
skipKey "monitor"
h <- header
mc <- optionMaybe $ char '"' >> many (noneOf "\n\"") << skipChar '"'
f <- foltl
return $ Monitor h mc f
mon :: CharParser st [Monitor]
mon = many1 monitor
aBoxLit :: CharParser st ABoxLit
aBoxLit = pair (option True $ skipKey "not" >> return False) abox
aBoxDelete :: CharParser st (Either ABoxLit [String])
aBoxDelete = fmap Right (skipKey "delete" >> parent (sepBy nominal commaP))
<|> fmap Left aBoxLit
aBoxDeletes :: CharParser st ABoxDeletes
aBoxDeletes = sepBy aBoxDelete commaP
aBoxs :: CharParser st ABoxs
aBoxs = sepBy abox commaP
aBoxAnds :: CharParser st ABoxAnds
aBoxAnds = sepBy1 aBoxLit (skipKey "and")
condEffect :: CharParser st CondEffect
condEffect = skipKey "fi" >> liftM2 CondEffect (parent abox) aBoxDeletes
skipEqKey :: String -> CharParser st ()
skipEqKey s = skipKey s >> equalP
exec :: CharParser st Exec
exec = do
let name = many1 myLetter
skipEqKey "exec"
s1 <- name << char '.'
s2 <- name << skip
ps <- optNoms
return $ Exec s1 s2 ps
action :: CharParser st Action
action = do
h <- skipKey "action" >> header
braced $ do
as <- optionL $ skipEqKey "pre" >> aBoxs
ds <- optionL $ skipEqKey "effect" >> aBoxDeletes
cs <- many condEffect
me <- optionMaybe exec
return $ Action h as ds cs me
indEffect :: CharParser st IndEffect
indEffect = do
s <- skipKey "indirect" >> skipKey "effect" >> nominal
equalP
braced $ do
as <- skipEqKey "init" >> aBoxs
cs <- skipEqKey "causes" >> aBoxDeletes
ds <- skipEqKey "cond" >> aBoxs
return $ IndEffect s as cs ds
process :: CharParser st Process
process = skipKey "function" >> liftM2 Process header proc
primProc, starProc, preProc, semiProc, pipeProc, proc :: CharParser st Proc
primProc = braced proc
<|> parent proc
<|> fmap Init (try $ skipKey "init" >> foltl)
<|> fmap Quest (try $ abox << skipChar '?')
<|> liftM2 CallP
(reserved ["while", "forall", "if", "switch"] nominal)
optNoms
starProc = do
p <- primProc
ls <- many $ skipChar '*'
return $ if null ls then p else Star p
preProc = liftM2 While (skipKey "while" >> parent aBoxAnds) proc
<|> liftM3 IfElse (skipKey "if" >> parent aBoxAnds) proc
(skipKey "else" >> proc)
<|> liftM2 Forall (skipKey "forall" >> aBoxAnds) (impliesP >> proc)
<|> fmap Switch (skipKey "switch" >>
many (skipKey "case" >> pair
(fmap Just aBoxAnds <|> (skipChar '_' >> return Nothing))
(impliesP >> proc)))
<|> starProc
semiProc = binC (BinP Semi) ';' preProc
pipeProc = binC (BinP Pipe) '|' semiProc
proc = binPP (BinP SeqPlus) ((tryString "+>" >> skip) <|> skipChar '+') pipeProc
ship :: CharParser st Ship
ship = fmap ShipP process <|> fmap ShipA action <|> fmap ShipI indEffect
ships :: CharParser st [Ship]
ships = many ship