-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.hs
304 lines (249 loc) · 8.57 KB
/
verifier.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
import Data.Maybe
import Data.List
import Control.Monad
type World = [Char]
type State = [Char]
type Relations = [(World, World)]
type Valuations = [(String, [World])]
type KripkeFrame = ([World], Relations)
type KripkeModel = (KripkeFrame, Valuations)
type CtlModel = ([State], Relations, Valuations)
-- TODO: Weak Until.
data Exp = Constant Bool
| Variable String
| Not Exp
| Box Exp
| Diamond Exp
| And Exp Exp
| Or Exp Exp
| IfElse Exp Exp
| Until Exp Exp
| A Exp
| G Exp
| F Exp
| X Exp
| E Exp
deriving (Show, Eq, Read)
-- TODO: Epistemic Modalities
--data LtlKExp = Variable String
-- | Not LtlKExp
-- | And LtlKExp LtlKExp
-- | X LtlKExp
-- | U LtlKExp LtlKExp
-- | K tlKExp
-- | E LtlKExp
-- | D LtlKExp
-- | C LtlKExp
ctlModel = (["s0","s1","s2","s3"],
[("s0","s3"),("s0","s1"),("s1","s1"),("s1","s2"),("s2","s0"),("s2","s3"),("s3","s0")],
[("p",["s0","s2"]),("q",["s0","s3"]),("r",["s3","s1"]),("t",["s2"])])
relations = [("w2", "w1"),
("w3", "w2"),
("w4", "w2"),
("w4", "w3"),
("w3", "w3")]
q1relations = [("s0","s0"),("s0","s1"),("s0","s3"),("s1","s2"),("s2","s1"),("s3","s2")]
q1valuations = [("p",["s1","s3"]),("q",["s2","s3"]),("r",["s0","s1","s2"]),("t",["s1"])]
q1world = ["s0","s1","s2","s3"]
worlds = ["w1", "w2", "w3", "w4"]
valuations = [("p", ["w2", "w4"]),
("q", ["w2", "w3", "w4"])]
kripkeFrame = (worlds, relations)
kripkeModel = (kripkeFrame, valuations)
exp1 = Box (Variable "p")
exp2 = Diamond (Variable "p")
exp3 = IfElse
(IfElse
(Diamond (Variable "q"))
(Diamond (Not (Variable "p"))))
(And
(Not (Variable "p"))
(Not (Variable "q")))
exp4 = Box (Constant False)
exp5 = Diamond (Constant False)
exp6 = And (Box (Constant True)) (Box (Constant False))
exp7 = Or (Diamond (Constant True)) (Diamond (Constant False))
ctlExp1 = (A . F) (Variable "q")
ctlExp2 = (A . G . E . F ) (Or (Variable "p") (Variable "q"))
ctlExp3 = (E . X . E . X) (Variable "r")
ctlExp4 = (A . G . A . F) (Variable "q")
ctlExp5 = E (Until (Variable "p") ((Not . A . F) (Variable "q")))
w1 = "w1"
w2 = "w2"
w3 = "w3"
w4 = "w4"
---------------------------------------------------------------------
lookUp x xs = fromJust (lookup x xs)
eval :: KripkeModel -> World -> Exp -> Bool
eval _ _ (Constant True) = True
eval _ _ (Constant False) = False
eval (((ws', rs), vs)) w (Box e)
= and (map f ws)
where
f w' = eval (((ws', rs), vs)) w' e
ws = relatedTo w rs
eval k w (Diamond e) = not (eval k w (Box (Not e)))
eval ((_, vs)) w (Variable p) = elem w (lookUp p vs)
eval k w (Not e) = not (eval k w e)
eval k w (And e1 e2) = (eval k w e1) && (eval k w e2)
eval k w (Or e1 e2) = (eval k w e1) || (eval k w e2)
eval k w (IfElse e1 e2)
| eval k w e1 = eval k w e2
| otherwise = True
relatedTo :: World -> Relations -> [World]
relatedTo w [] = []
relatedTo w ((w', w''):ws)
| w == w' = w'' : relatedTo w ws
| otherwise = relatedTo w ws
-- Determines the set of states satisfying EX e.
satEX e m
= nub [s' | (s', _) <- rs]
where
x = sat e m
(s, rs, vs) = m
-- Builds a map from a state to all states it is related to.
buildRelationMap :: [(State, State)] -> [(State, [State])] -> [(State, [State])]
buildRelationMap [] m
= m
buildRelationMap ((k,v):rs) []
= buildRelationMap rs [(k,[v])]
buildRelationMap ((k,v):rs) ((k',vs):rs')
| k == k' = buildRelationMap rs ((k,(v:vs)):rs')
| otherwise = buildRelationMap rs ((k,[v]):(k',vs):rs')
getStatesAll' :: [(State, [State])] -> [State] -> [State]
getStatesAll' [] _ = []
getStatesAll' ((k,vs):rest) y
| and (map f vs) = k : getStatesAll' rest y
| otherwise = getStatesAll' rest y
where
f v = elem v y
getStatesAll :: Relations -> [State] -> [State]
getStatesAll rs y
= getStatesAll' values y
where
values = buildRelationMap rs []
satAF' :: [State] -> [State] -> Relations -> [State]
satAF' x y rs
| x == y = y
| otherwise = satAF' x' y' rs
where
x' = y
y' = union y (getStatesAll rs y)
-- Determines the set of states satisfying AF e.
satAF :: Exp -> CtlModel -> [State]
satAF e m
= satAF' x y rs
where
x = s
y = sat e m
(s, rs, _) = m
getStates :: Relations -> [State] -> [State]
getStates [] _ = []
getStates ((s,s'):ss) y
| elem s' y = s : (getStates ss y)
| otherwise = getStates ss y
satEU' :: [State] -> [State] -> [State] -> CtlModel -> [State]
satEU' x y w m
| x == y = y
| otherwise = satEU' x' y' w m
where
x' = y
y' = union y (intersect w (nub (getStates rs y)))
(_, rs, _) = m
-- Determines the states satisfying E[e1 U e2].
satEU :: Exp -> Exp -> CtlModel -> [State]
satEU e1 e2 m
= satEU' x y w m
where
w = sat e1 m
x = s
y = sat e2 m
(s, _, _) = m
sat :: Exp -> CtlModel -> [State]
sat (Constant True) (s, _, _) = s
sat (Constant False) _ = []
sat (Variable p) (s, _, vs) = [s' | s' <- lookUp p vs]
sat (Not e) (s, rs, vs) = (\\) s (sat e (s, rs, vs))
sat (And e1 e2) m = intersect (sat e1 m) (sat e2 m)
sat (Or e1 e2) m = union (sat e1 m) (sat e2 m)
sat (IfElse e1 e2) m = sat (Or (Not e1) e2) m
sat (A (X e)) m = sat ((Not . E . X . Not) e) m
sat (A (Until e1 e2)) m
= sat (Not (Or (E ( Until (Not e2) (And (Not e1) (Not e2)))) ((E . G . Not) e2))) m
sat (E (F e)) m = sat (E (Until (Constant True) e)) m
sat (E (G e)) m = sat ((Not . A . F) (Not e)) m
sat (A (G e)) m = sat ((Not . E . F) (Not e)) m
sat (E (X e)) m = satEX e m
sat (A (F e)) m = satAF e m
sat (E (Until e1 e2)) m = satEU e1 e2 m
printRelation relation = do
putStrLn $ fst relation ++ " -> " ++ snd relation
printPi (p, vs) = do
putStrLn $ "pi(" ++ p ++ ") = { " ++ unwords vs ++ " }"
doCtl = do
putStrLn "Enter the set of states in the form 's0 s1 s2 ...' :"
statesStr <- getLine
let states = words statesStr
relations <- forM states (\state -> do
putStrLn $ "What is " ++ state ++ " related to?"
relatedTo <- getLine
return (map (\x -> (state, x)) (words relatedTo)) )
putStrLn "The relations are: "
mapM printRelation $ concat relations
putStrLn "Enter the set of atoms in the form 'p q r ...' :"
atoms <- getLine
valuations <- forM (words atoms) (\atom -> do
putStrLn $ "At which states are " ++ atom ++ " true?"
valuation <- getLine
return (atom, words valuation))
putStrLn "pi is defined as: "
mapM printPi valuations
putStrLn "Enter the formula to evaluate: "
expStr <- getLine
putStrLn $ "The set of states on which the formula is satisfied are:"
putStrLn $ unwords $ sat (read expStr :: Exp) (states, concat relations, valuations)
doKripke = do
putStrLn "TODO: Kripke I/O"
main = do
putStrLn "Enter an integer to choose the desired mode."
putStrLn "---------------------"
putStrLn " (1) Kripke Models"
putStrLn " (2) CTL"
putStrLn "---------------------"
option <- getLine
if option == "1"
then doKripke
else doCtl
showExp :: Exp -> String
showExp (Variable p) = p
showExp (Constant c) = show c
showExp (A e) = "A ( " ++ showExp(e) ++ ")"
showExp (G e) = "G ( " ++ showExp(e) ++ ")"
showExp (F e) = "F ( " ++ showExp(e) ++ ")"
showExp (X e) = "X ( " ++ showExp(e) ++ ")"
showExp (E e) = "E ( " ++ showExp(e) ++ ")"
showExp (Not e) = "Not (" ++ showExp(e) ++ ")"
showExp (Diamond e) = "<> (" ++ showExp(e) ++ ")"
showExp (Box e) = "[] (" ++ showExp(e) ++ ")"
showExp (And e1 e2) = "(" ++ showExp(e1) ++ ") And (" ++ showExp(e2) ++ ")"
showExp (Or e1 e2) = "(" ++ showExp(e1) ++ ") Or (" ++ showExp(e2) ++ ")"
showExp (Until e1 e2) = "(" ++ showExp(e1) ++ ") Until (" ++ showExp(e2) ++ ")"
showExp (IfElse e1 e2) = "(" ++ showExp(e1) ++ ") -> (" ++ showExp(e2) ++ ")"
---------------------------------------------------------------------
runKripkeTests
= eval kripkeModel w4 exp1 == False &&
eval kripkeModel w4 exp2 == True &&
eval kripkeModel w3 exp1 == False &&
eval kripkeModel w3 exp3 == False &&
eval kripkeModel w2 exp4 == False &&
eval kripkeModel w2 exp5 == False &&
eval kripkeModel w1 exp6 == True &&
eval kripkeModel w2 exp7 == True
runSatTests
= sort (sat ctlExp1 ctlModel) == ["s0","s2","s3"] &&
sort (sat ctlExp2 ctlModel) == ["s0","s1","s2","s3"] &&
sort (sat ctlExp3 ctlModel) == ["s0","s1","s2","s3"] &&
sort (sat ctlExp4 ctlModel) == [] &&
sort (sat ctlExp5 ctlModel) == ["s0", "s1", "s2"]
runAllTests
= runKripkeTests && runSatTests