-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bisim.hs
295 lines (238 loc) · 7.01 KB
/
Bisim.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
{-# LANGUAGE TypeFamilies, FlexibleContexts, RankNTypes #-}
module Bisim 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 )
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
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 ++ ")"
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))
]
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 ]
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
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.
-}