-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.hs
418 lines (350 loc) · 17.2 KB
/
Core.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
module Core where
import Data.Set(Set, singleton, delete, insert, map, empty, elemAt, toList, member)
import Data.List(nub,sort)
import Control.Monad(when,forM_)
import StateMonad
-- Kripke structure states are ints.
type State = Int
{-
KS (n, l, r), where 'n' indicates the states [0 .. n], 'l' is the labeling function,
and 'r' is the transition relation.
-}
data KripkeS = KS (Int, State->[State], State->(At->Bool))
-- Propositional vars are strings.
type At = String
-- State forms.
data StateF = Var At
| Neg At
| ConjS StateF StateF
| DisjS StateF StateF
| A PathF
| E PathF deriving (Eq, Ord)
-- Path forms.
data PathF = St StateF
| DisjP PathF PathF
| ConjP PathF PathF
| U PathF PathF
| V PathF PathF
| X PathF deriving (Eq,Ord)
negS::StateF->StateF
negS φ = case φ of
Var a -> Neg a
Neg a -> Var a
ConjS φ₁ φ₂ -> DisjS (negS φ₁) (negS φ₂)
DisjS φ₁ φ₂ -> ConjS (negS φ₁) (negS φ₂)
A ф -> E $ negP ф
E ф -> A $ negP ф
negP::PathF->PathF
negP ф = case ф of
St φ -> St $ negS φ
ConjP ф₁ ф₂ -> DisjP (negP ф₁) (negP ф₂)
DisjP ф₁ ф₂ -> ConjP (negP ф₁) (negP ф₂)
X ф₁ -> X $ negP ф₁
U ф₁ ф₂ -> V (negP ф₁) (negP ф₂)
V ф₁ ф₂ -> U (negP ф₁) (negP ф₂)
bot = Var ""
top = Neg ""
opG::PathF->PathF
opG ф = case ф of
-- GGф ≡ Gф
V (St (Var "")) ф₁ -> opG ф₁
-- GFGф ≡ FGф
U (St (Neg "")) (V (St (Var "")) ф₁) -> opF $ opG $ ф₁
_ -> V (St bot) ф
opF::PathF->PathF
opF ф = case ф of
-- FFф ≡ Fф
U (St (Neg "")) ф₁ -> opF ф₁
-- FGFф ≡ GFф
V (St (Var "")) (U (St (Neg "")) ф₁) -> opG $ opF ф₁
_ -> U (St top) ф
impP::PathF->PathF->PathF
impP ф₁ ф₂ = if ф₁==ф₂ then St top else DisjP (negP ф₁) ф₂
impS::StateF->StateF->StateF
impS φ₁ φ₂ = if φ₁==φ₂ then top else DisjS (negS φ₁) φ₂
data Assertion = Assrt (State, Set PathF) deriving (Eq, Ord)
deleteF::PathF->Assertion->Assertion
deleteF ф (Assrt (s,_Φ)) = Assrt (s,delete ф _Φ)
insertF::PathF->Assertion->Assertion
insertF ф (Assrt (s,_Φ)) = Assrt (s,insert ф _Φ)
data Subgoals = T | Subg [Assertion] deriving Show
subgoals::KripkeS->Assertion->Subgoals
subgoals ks@(KS (_,r,_)) σ@(Assrt (s,_Φ)) =
if _Φ == empty
then Subg []
else let ф = elemAt 0 _Φ in
case ф of
St φ -> if eval_modchkCTLS (ks,s) φ then T else Subg [deleteF ф σ]
DisjP ф₁ ф₂ -> Subg [insertF ф₁ $ insertF ф₂ $ deleteF ф σ]
ConjP ф₁ ф₂ -> -- ф∧ф ≡ ф
if ф₁==ф₂
then Subg [insertF ф₁ $ deleteF ф σ]
else Subg [insertF ф₁ $ deleteF ф σ,
insertF ф₂ $ deleteF ф σ]
U ф₁ ф₂ -> -- фUф ≡ ф
if ф₁==ф₂
then Subg [insertF ф₁ $ deleteF ф σ]
else -- ф₁Uф₂ ≡ (ф₁∨ф₂)∧(ф₂∨(X(ф₁Uф₂)))
Subg [insertF ф₁ $ insertF ф₂ $ deleteF ф σ,
insertF ф₂ $ insertF (X ф) $ deleteF ф σ]
V ф₁ ф₂ -> -- фRф ≡ ф
if ф₁==ф₂
then Subg [insertF ф₁ $ deleteF ф σ]
else -- ф₁Rф₂ ≡ ф₂∧(ф₁∨(X(ф₁Rф₂)))
Subg [insertF ф₂ $ deleteF ф σ,
insertF ф₁ $ insertF (X ф) $ deleteF ф σ]
X _ -> -- (Xф₁)∨(Xф₂)∨⋯∨(Xф_n) ≡ X(ф₁∨ф₂∨⋯∨ф_n)
let _Φ₁ = Data.Set.map (\(X ф) -> ф) _Φ in
Subg [Assrt (s',_Φ₁) | s' <- r s]
check_success::[Assertion]->Bool
check_success v = let фs = concat [toList _Φ | Assrt (_,_Φ) <- v] in
(not . null) [V ф₁ ф₂ | V ф₁ ф₂ <- фs, (not . elem ф₂) фs]
{- Strongly Connected Components -}
type DFSn = Int
type Low = Int
type Valid = [(PathF, Int)]
type Stack = [Assertion]
type V = Set Assertion
type F = Set Assertion
type StateDFS = (DFSn, Assertion->(DFSn, Low, Valid), Stack, V, F, Bool)
-- initial state of dfs.
init_state = (0, \_ -> (0, 0, []), [], empty, empty, False)
pushS::Assertion->StateM StateDFS ()
pushS σ = ST $ \(dfsn, i, stack, v, f, b) -> ((), (dfsn, i, σ:stack, v, f, b))
popS::StateM StateDFS Assertion
popS = ST $ \(dfsn, i, σ:stack, v, f, b) -> (σ, (dfsn, i, stack, v, f, b))
inStack::Assertion->StateM StateDFS Bool
inStack σ = ST $ \(dfsn, i, stack, v, f, b) -> (elem σ stack, (dfsn, i, stack, v, f, b))
get_stack::StateM StateDFS Stack
get_stack = ST $ \(dfsn, i, stack, v, f, b) -> (stack, (dfsn, i, stack, v, f, b))
insert_V::Assertion->StateM StateDFS ()
insert_V σ = ST $ \(dfsn, i, stack, v, f, b) -> ((), (dfsn, i, stack, insert σ v, f, b))
elem_V::Assertion->StateM StateDFS Bool
elem_V σ = ST $ \(dfsn, i, stack, v, f, b) -> (member σ v, (dfsn, i, stack, v, f, b))
insert_F::Assertion->StateM StateDFS ()
insert_F σ = ST $ \(dfsn, i, stack, v, f, b) -> ((), (dfsn, i, stack, v, insert σ f, b))
elem_F::Assertion->StateM StateDFS Bool
elem_F σ = ST $ \(dfsn, i, stack, v, f, b) -> (member σ f, (dfsn, i, stack, v, f, b))
set_flag::Bool->StateM StateDFS ()
set_flag b = ST $ \(dfsn, i, stack, v, f, _) -> ((), (dfsn, i, stack, v, f, b))
get_flag::StateM StateDFS Bool
get_flag = ST $ \(dfsn, i, stack, v, f, b) -> (b, (dfsn, i, stack, v, f, b))
get_dfsn::Assertion->StateM StateDFS DFSn
get_dfsn σ = ST $ \(dfsn, i, stack, v, f, b) -> let (d,_,_) = i σ in
(d, (dfsn, i, stack, v, f, b))
get_low::Assertion->StateM StateDFS Low
get_low σ = ST $ \(dfsn, i, stack, v, f, b) -> let (_,lo,_) = i σ in
(lo, (dfsn, i, stack, v, f ,b))
set_low::Assertion->Low->StateM StateDFS ()
set_low σ lo = ST $ \(dfsn, i, stack, v, f, b) -> ((), (dfsn, \σ_ -> if σ_ == σ
then let (df, _, va) = i σ in
(df, lo, va)
else i σ_, stack, v, f, b))
get_valid::Assertion->StateM StateDFS Valid
get_valid σ = ST $ \(dfsn, i, stack, v, f, b) -> let (_,_,va) = i σ in
(va, (dfsn, i, stack, v, f, b))
set_valid::Assertion->Valid->StateM StateDFS ()
set_valid σ val = ST $ \(dfsn, i, stack, v, f, b) -> ((),(dfsn,\σ_ -> if σ_ == σ
then let (df,lo, _) = i σ in
(df, lo, val)
else i σ_, stack, v, f, b))
init::(Assertion,Valid)->StateM StateDFS ()
init (σ@(Assrt (_,_Φ)), valid) =
ST $ \(dfsn, i, stack, v, f, b) ->
((), (dfsn+1, \σ1 -> if σ1 == σ
then (dfsn+1, dfsn+1, let фs = toList _Φ in
init_valid (dfsn+1)
(nub $
[V ф₁ ф₂ | V ф₁ ф₂ <- фs, (not . elem ф₂) фs] ++
[V ф₁ ф₂ | X (V ф₁ ф₂) <- фs, (not . elem ф₂) фs]))
else i σ1, stack, v, f, b))
where
init_valid dfsn rs = case rs of
[] -> []
ф:фs -> let x = [sp | (ф_,sp) <- valid, ф_==ф] in
case x of
[] -> (ф, dfsn):(init_valid dfsn фs)
sp:_ -> (ф, sp):(init_valid dfsn фs)
dfs::(Assertion,Valid)->KripkeS->StateM StateDFS Bool
dfs (σ,valid) ks =
do
set_flag True
Core.init(σ,valid)
pushS σ
insert_V σ
case subgoals ks σ of
T -> set_flag True
Subg σs -> case σs of
[] -> set_flag False
_ -> forM_ σs (\σ1 -> do
flag <- get_flag
when flag $ do
σ1_in_V <- elem_V σ1
if σ1_in_V
then do
σ1_in_F <- elem_F σ1
if σ1_in_F
then set_flag False
else do
σ1_in_stack <- inStack σ1
when σ1_in_stack
(do
σ_low <- get_low σ
σ1_low <- get_low σ1
set_low σ (min σ_low σ1_low)
σ_valid <- get_valid σ
σ1_dfsn <- get_dfsn σ1
set_valid σ [(r,sp) | (r,sp) <- σ_valid, sp <= σ1_dfsn]
σ_valid <- get_valid σ
when (null σ_valid)
(set_flag False))
else do
σ_valid <- get_valid σ
flag_dfs <- dfs(σ1,σ_valid) ks
set_flag flag_dfs
σ1_low <- get_low σ1
σ_dfsn <- get_dfsn σ
when (σ1_low <= σ_dfsn)
(do
σ_low <- get_low σ
set_low σ (min σ_low σ1_low)
σ1_valid <- get_valid σ1
set_valid σ σ1_valid))
σ_dfsn <- get_dfsn σ
σ_low <- get_low σ
when (σ_dfsn == σ_low)
(do
stack <- get_stack
let stack' = σ : takeWhile (σ /=) stack
forM_ stack' (\_ -> popS)
flag <- get_flag
when (not flag)
(forM_ stack' (\σ_ -> insert_F σ_)))
flag <- get_flag;
return flag
-- LTL model checker.
modchkLTL::Assertion->KripkeS->StateM StateDFS Bool
modchkLTL σ ks = dfs(σ,[]) ks
eval_modchkLTL::Assertion->KripkeS->Bool
eval_modchkLTL σ ks = evalStateM (modchkLTL σ ks) init_state
-- CTL★ model checker.
modchkCTLS::Assertion->KripkeS->StateM StateDFS Bool
modchkCTLS σ@(Assrt (s,_Φ)) ks@(KS (_,_,l)) =
do
σ_in_V <- elem_V σ
if σ_in_V
then do
σ_in_F <- elem_F σ
if σ_in_F
then set_flag False
else set_flag True
else insert_V σ
let St φ = elemAt 0 _Φ
case φ of
Var a -> update (l s a)
Neg a -> update ((not . l s) a)
ConjS φ₁ φ₂ -> do
b1 <- modchkCTLS (Assrt (s,singleton $ St φ₁)) ks
b2 <- modchkCTLS (Assrt (s,singleton $ St φ₂)) ks
update (b1 && b2)
DisjS φ₁ φ₂ -> do
b1 <- modchkCTLS (Assrt (s,singleton $ St φ₁)) ks
b2 <- modchkCTLS (Assrt (s,singleton $ St φ₂)) ks
update (b1 || b2)
A ф -> do
b <- modchkLTL (Assrt (s,singleton ф)) ks
update b
E ф -> do
b <- modchkLTL (Assrt (s,(singleton . negP) ф)) ks
update (not b)
flag <- get_flag
return flag
where
update b = do {set_flag b;when (not b) (insert_F σ)}
eval_modchkCTLS::(KripkeS,State)->StateF->Bool
eval_modchkCTLS (ks,s) φ = evalStateM (modchkCTLS (Assrt (s, singleton $ St φ)) ks) init_state
{-====================================================================================-}
{- Show instances -}
instance Show Assertion where
show (Assrt (s,_Φ)) = "s" ++ show s ++ " ⊢ " ++ (show $ toList _Φ)
instance Show StateF where
show sf = case sf of
-- Variables
Var "" -> "⊥"
Var a -> a
Neg "" -> "┬"
Neg a -> "¬"++a
-- Conjunction
ConjS (Var p) (Var q) -> p++" ⋀ "++q
ConjS (Neg p) (Neg q) -> "¬"++p++" ⋀ ¬"++q
ConjS s1 (Var q) -> case s1 of
Neg p -> show s1++" ⋀ "++q
_ -> "("++show s1++") ⋀ "++q
ConjS (Var p) s2 -> case s2 of
Neg q -> p++" ⋀ ¬"++q
_ -> p++" ⋀ ("++show s2++")"
ConjS s1@(Neg p) s2 -> show s1++" ⋀ ("++show s2++")"
ConjS s1 s2@(Neg q) -> "("++show s1++") ⋀ "++show s2
ConjS s1 s2 -> "("++show s1++") ⋀ ("++show s2++")"
-- Disjunction
DisjS (Var p) (Var q) -> p++" ⋁ "++q
DisjS (Neg p) (Neg q) -> "¬"++p++" ⋁ ¬"++q
DisjS s1 (Var q) -> case s1 of
Neg p -> show s1++" ⋁ "++q
_ -> "("++show s1++") ⋁ "++q
DisjS (Var p) s2 -> case s2 of
Neg q -> p++" ⋁ ¬"++q
_ -> p++" ⋁ ("++show s2++")"
DisjS s1@(Neg p) s2 -> show s1++" ⋁ ("++show s2++")"
DisjS s1 s2@(Neg q) -> "("++show s1++") ⋁ "++show s2
DisjS s1 s2 -> "("++show s1++") ⋁ ("++show s2++")"
-- ForAll
A p -> case p of
X p' -> "AX "++show p'
U (St (Neg "")) p' -> "AF "++show p'
V (St (Var "")) p' -> "AG "++show p'
_ -> "A["++show p++"]"
-- Exists
E p -> case p of
X p' -> "EX "++show p'
U (St (Neg "")) p' -> "EF "++show p'
V (St (Var "")) p' -> "EG "++show p'
_ -> "E["++show p++"]"
instance Show PathF where
show p = case p of
-- State Formulas
St s -> case s of
Var _ -> show s
Neg _ -> show s
_ -> "("++show s++")"
-- Conjunction
ConjP p1@(St _) p2@(St _) -> show p1++" ⋀ "++show p2
ConjP p1@(St _) p2 -> show p1++" ⋀ ("++show p2++")"
ConjP p1 p2@(St _) -> "("++show p1++") ⋀ "++show p2
ConjP p1 p2 -> "("++show p1++") ⋀ ("++show p2++")"
-- Disjunction
DisjP p1@(St _) p2@(St _) -> show p1++" ⋁ "++show p2
DisjP p1@(St _) p2 -> show p1++" ⋁ ("++show p2++")"
DisjP p1 p2@(St _) -> "("++show p1++") ⋁ "++show p2
DisjP p1 p2 -> "("++show p1++") ⋁ ("++show p2++")"
-- neXt state
X q -> case q of
St s@(Var _) -> "X"++show s
St s@(Neg _) -> "X"++show s
St s@(_) -> "X"++show q
X q1 -> "X"++show q
_ -> "X("++show q++")"
-- Until
U (St (Neg "")) p2@(St _) -> "F"++show p2
U (St (Neg "")) p2 -> "F("++show p2++")"
U p1@(St _) p2@(St _) -> show p1++" U "++show p2
U p1@(St _) p2 -> show p1++" U ("++show p2++")"
U p1 p2@(St _) -> "("++show p1++") U "++show p2
U p1 p2 -> "("++show p1++") U ("++show p2++")"
-- Velease
V (St (Var "")) p2@(St _) -> "G"++show p2
V (St (Var "")) p2 -> "G("++show p2++")"
V p1@(St _) p2@(St _) -> show p1++" V "++show p2
V p1@(St _) p2 -> show p1++" V ("++show p2++")"
V p1 p2@(St _) -> "("++show p1++") V "++show p2
V p1 p2 -> "("++show p1++") V ("++show p2++")"