-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiSimWed.hs
388 lines (312 loc) · 9.48 KB
/
BiSimWed.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
{-# LANGUAGE TypeFamilies, FlexibleContexts, RankNTypes #-}
module BiSimWed where
import Test.QuickCheck
import Data.Maybe
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Set( Set )
import System.Cmd
import Data.Tree
import Data.Tree.Pretty
class (Show p, Ord p) => Proc p where
type Event p
step :: p -> [(Event p, p)]
(~~) :: (Proc p, Ord (Event p)) => p -> p -> Bool
p ~~ q =
rep rel p == rep rel q
where
top = explore (-1) S.empty [p,q]
rel = fix id refRel [top] -- N.b.: not fix length!
ork = 1000
(~^~) :: (Proc p, Ord (Event p)) => p -> p -> Property
p ~^~ q
| S.size top < ork =
property $ rep rel p == rep rel q
| otherwise =
False ==> True
where
top = explore ork S.empty [p,q]
rel = fix id refRel [top] -- N.b.: not fix length!
explore :: Proc p => Int -> Set p -> [p] -> Set p
explore 0 seen _ = seen
explore n seen [] = seen
explore n seen (p:ps)
| p `S.member` seen = explore n seen ps
| otherwise = explore (n-1) (p `S.insert` seen) (map snd (step p) ++ ps)
refClass :: (Proc p, Ord (Event p)) => (p -> p) -> Set p -> [Set p]
refClass currrep ps =
[ S.fromList qs
| (_,qs) <- M.toList table
]
where
rep' p = map head $ group $ sort [ (e,currrep p') | (e,p') <- step p ]
table = M.fromListWith (++) [ (rep' p, [p]) | p <- S.toList ps ]
refRel :: (Proc p, Ord (Event p)) => [Set p] -> [Set p]
refRel pss =
[ qs
| ps <- pss
, qs <- refClass (rep pss) ps
, S.size qs > 1
]
rep :: Proc p => [Set p] -> (p -> p)
rep pss p = head $
[ S.findMin ps
| ps <- pss
, p `S.member` ps
] ++
[ p ] -- why is this needed? (because of the singleton-dropping optimization)
fix h f x
| h x == h fx = x
| otherwise = fix h f fx
where
fx = f x
-----------------------------------------------------
-- CCS (a la Koen)
data Msg
= In Char
| Out Char
deriving ( Eq, Ord )
dual :: Msg -> Msg
dual (In a) = Out a
dual (Out a) = In a
instance Show Msg where
show (In a) = [a,'?']
show (Out a) = [a,'!']
instance Arbitrary Msg where
arbitrary =
do a <- growingElements ['a'..'h']
b <- arbitrary
return (if b then In a else Out a)
shrink (In a) = [ Out b | b <- [a,succ a] ] ++
[ In a' | a' <- shrink a ]
shrink (Out a) = [ Out a' | a' <- shrink a ]
data CCSEvent
= Do Msg | Tau
deriving (Eq, Ord)
instance Show CCSEvent where
show Tau = "_."
show (Do m) = show m
instance Arbitrary CCSEvent where
arbitrary = frequency [(1,return Tau),(3,arbitrary)]
shrink Tau = []
shrink (Do m) = Tau : map Do (shrink m)
matches Tau _ = False
matches _ Tau = False
matches (Do a) (Do b) = a == dual b
data P
= Nil
| Act CCSEvent P
| P :+: P
| P :|: P
| Star P
deriving ( Eq, Ord )
instance Show P where
show Nil = "0"
show (Act m p) = show m ++ show p
show (p :+: q) = "(" ++ show p ++ "+" ++ show q ++ ")"
show (p :|: q) = "(" ++ show p ++ " | " ++ show q ++ ")"
show (Star p) = "(" ++ show p ++ ")*"
instance Arbitrary P where
arbitrary = sized (arbP . (`div` 3))
where
arbP n =
frequency
[ (1, return Nil)
, (k, do m <- arbitrary
p <- arbP n1
return (Act m p))
, (k, do p <- arbP n2
q <- arbP n2
op <- elements [(:|:),(:+:)]
return (p `op` q))
, (1, do p <- arbP n1
return (Star p))
]
where
k = 5 `min` n
n1 = n-1
n2 = n `div` 2
shrink Nil = []
shrink (Act m p) = [ p ] ++
[ Act m' p | m' <- shrink m ] ++
[ Act m p' | p' <- shrink p ]
shrink (p :+: q) = [ p, q ] ++
[ p' :+: q | p' <- shrink p ] ++
[ p :+: q' | q' <- shrink q ]
shrink (p :|: q) = [ p, q, p :+: q ] ++
[ p' :|: q | p' <- shrink p ] ++
[ p :|: q' | q' <- shrink q ]
shrink (Star p) = [ p ] ++ map Star (shrink p)
instance Proc P where
type Event P = CCSEvent
step Nil =
[]
step (Act m p) =
[(m, p)]
step (p :+: q) =
step p ++ step q
step (p :|: q) =
[ (Tau, p' :|: q')
| (a, p') <- ps
, (b, q') <- qs
, matches a b
] ++
[ (m, p' :|: q)
| (m, p') <- ps
] ++
[ (m, p :|: q')
| (m, q') <- qs
]
where
ps = step p
qs = step q
step (Star p) =
[ (e,p' :|: Star p) | (e,p') <- ps] ++
[ (Tau,p' :|: q' :|: Star p)
| (a,p') <- ps,
(b,q') <- ps,
matches a b ]
where
ps = step p
type At p = forall a b. ((p -> a) -> b) -> (p -> a) -> b
atP :: At P
atP = id
atCCSEvent :: At Msg
atCCSEvent = id
prop_Refl p =
p ~^~ p
prop_Plus_Nil p =
(Nil :+: p) ~^~ p
prop_Par_Nil p =
(Nil :|: p) ~^~ p
-- this is very slow to test!
prop_Par_commutes p q =
(p :|: q) ~^~ (q :|: p)
prop_Par_commutes_state_space p q =
collect (S.size $ explore (-1) S.empty [p :|: q,q :|: p]) True
prop_Par_associates p q r =
(p :|: (q :|: r)) ~^~ ((p :|: q) :|: r)
prop_Wrong p q =
expectFailure $
p ~^~ q
prop_Wrong2 m p q =
expectFailure $
Act m (p :+: q) ~^~ (Act m p :+: Act m q)
prop_Wrong3 m p q =
expectFailure $
Act m (p :|: q) ~^~ (Act m p :|: Act m q)
prop_Wrong4 p q r s =
(p :|: (q :|: (r :|: s))) ~^~ ((p :|: q) :|: r)
prop_Wrong5 p q r s =
(p :|: (q :|: (r :+: s))) ~~ (p :|: ((q :|: r) :+: (q :|: s)))
prop_Wrong6 p q r =
(p' :|: q') ~^~ (p' :|: r')
where p' = iterate (Act (Do (Out 'a'))) p !! hard
q' = iterate (Act (Do (In 'a')) Nil :|:) q !! hard
r' = iterate (Act (Do (In 'a')) Nil :|:) r !! hard
hard = 10
runPTests = do
atP qc prop_Refl
qc prop_Plus_Nil
qc prop_Par_Nil
qc prop_Par_commutes
-- qc prop_Par_commutes_state_space
qc prop_Par_associates
atP qc prop_Wrong
qc prop_Wrong2
qc prop_Wrong3
qc :: Testable a => a -> IO ()
qc = quickCheckWith stdArgs{maxSuccess=1000}
--- normalization of P
norm :: Int -> P -> P
norm 0 _ = Nil
norm n p
| null ps = Nil
| otherwise =
foldr1 (:+:) (map head . group . sort $
[Act m (norm (n-1) q) | (m,q) <- ps])
where ps = step p
prop_norm p q =
(p ~~ q) == (norm (-1) p == norm (-1) q)
prop_norm_implies_bisim p q =
norm 12 p == norm 12 q ==> p ~^~ q
{-
Experiments show that normalizing a process is too expensive. We
reasoned that, if we represent equivalence classes with unknown
members, then we must characterize the behaviour of those unknown
members with something equivalent to a
normalization-up-to-a-fixed-depth of the processes. This suggests that
using an exact equivalence relation is going to be too expensive.
So... suppose we do weaken the requirement that we are working with an
equivalence relation. An ER gives us a partition of the state space,
but suppose we settled for a covering of the state space instead? Then
when comparing two states, we would have ask not "do they have the
same representative", but "is there a set in the covering containing
both"? This is more expensive, but not catastrophically so... and we
hope may let us work with a restricted subset of the state space. The
next thing to try.
Additional thoughts (19/9): normalizing by floating Alts to the top is
stupid: we know Par will blow up that way. Can we "normalize" by
keeping Par at the top instead? We wouldn't get unique normal forms,
but maybe we can cope with that. The idea is to represent a set of
"non racing" events that a process can do in any order.
-}
data LTS = CutOff | N [(CCSEvent,LTS)]
deriving (Eq, Ord, Show)
unfold p = N $ [(e,unfold p') | (e,p') <- step p]
prune 0 (N eps) = CutOff
prune d (N eps) = N [(e,prune (if e==Tau then d else d-1) p') | (e,p')<-eps]
blur t = blur' t
where blur' 0 (N eps) = N []
blur' t' (N eps) = N [(e,blur' (if e==Tau then (t'-1) else t) p') | (e,p') <- eps]
canonize CutOff = CutOff
canonize (N eps) =
case eps' of
[(Tau,p)] -> p
_ ->
case [p | (Tau,p) <- eps',
p `canDo` (eps'\\[(Tau,p)])] of
[] -> N eps'
[p] -> p
where eps' = usort [(e,canonize p) | (e,p) <- eps]
CutOff `canDo` _ = True
N eps `canDo` eps' = null (eps' \\ eps)
tree d t p = canonize $ prune d $ blur t $ unfold p
usort xs = usort' (sort xs)
where usort' (x:y:xs) | x==y = usort' (y:xs)
usort' (x:xs) = x:usort' xs
usort' [] = []
cutoffs CutOff = [[]]
cutoffs (N eps) =
[ e:es
| (e,p) <- eps,
es <- cutoffs p ]
cutoff ps _ | [] `elem` ps = CutOff
cutoff ps CutOff = CutOff
cutoff ps (N eps) =
N [ (e,cutoff [es | e':es <- ps, e'==e] p)
| (e,p) <- eps ]
drawAct Tau = "tau"
drawAct (Do (Out a)) = a : "!"
drawAct (Do (In a)) = a : "?"
treeOf = treeOf' ""
where treeOf' l (N eps) = Node l [ treeOf' (drawAct e') p' | (e',p') <- eps ]
draw d t p = putStr $ drawTree $ treeOf $ tree d t p
p1 = (Act Tau (Act Tau Nil))
p2 = Star (Act Tau (Act (Do (Out 'a')) Nil))
p3 = Star (Act Tau (Act Tau (Act (Do (Out 'a')) Nil)))
-- draw trees
dot title t = do
writeFile ("tree"++title++".dot") $
"digraph{\nlabel=\""++title++"\";\nlabelloc=t;\n"++dot' 1 t++"}\n"
system $ "dot -Tjpg -O tree"++title++".dot"
system $ "tree"++title++".dot.jpg"
dot' i (N eps) =
"node"++show i++" [shape=point,label=\"\"];\n" ++
concat [ dot' (100*i+j) p++
"node"++show i++" -> node"++show (100*i+j)++"[label=\""++show e++"\"];\n"
| (j,(e,p)) <- zip [1..] eps
]
dot' i CutOff =
"node"++show i++" [shape=triangle,label=\"...\"];\n"