forked from apalache-mc/apalache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EWD998_buchi.tla
304 lines (251 loc) · 10.5 KB
/
EWD998_buchi.tla
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
------------------------------- MODULE EWD998_buchiFast -------------------------------
(***************************************************************************)
(* TLA+ specification of an algorithm for distributed termination *)
(* detection on a ring, due to Shmuel Safra, published as EWD 998: *)
(* Shmuel Safra's version of termination detection. *)
(* https://www.cs.utexas.edu/users/EWD/ewd09xx/EWD998.PDF *)
(***************************************************************************)
EXTENDS Integers, FiniteSets, Utils, Apalache
N == 5
Node == 0 .. N-1
Color == {"white", "black"}
Token == [pos : Node, q : Int, color : Color]
VARIABLES
\* @typeAlias: STATE = [ active: Int -> Bool, color: Int -> Str, counter: Int -> Int, pending: Int -> Int, token: [pos: Int, q: Int, color: Str]];
\* @type: Int -> Bool;
active, \* activation status of nodes
\* @type: Int -> Str;
color, \* color of nodes
\* @type: Int -> Int;
counter, \* nb of sent messages - nb of rcvd messages per node
\* @type: Int -> Int;
pending, \* nb of messages in transit to node
\* @type: [pos: Int, q: Int, color: Str];
token, \* token structure
\* @type: Bool;
InLoop,
(* loop variables *)
\* @type: Int -> Bool;
loop_active, \* activation status of nodes
\* @type: Int -> Str;
loop_color, \* color of nodes
\* @type: Int -> Int;
loop_counter, \* nb of sent messages - nb of rcvd messages per node
\* @type: Int -> Int;
loop_pending, \* nb of messages in transit to node
\* @type: [pos: Int, q: Int, color: Str];
loop_token, \* token structure
\* @type: Bool;
loop_fair,
\* @type: Int;
buchi_state,
\* @type: Int;
loop_buchi_state,
\* @type: Bool;
buchi_acceptingSeen,
\* @type: Bool;
PrevInLoop
vars == <<active, color, counter, pending, token>>
loop_vars == <<loop_active, loop_color, loop_counter, loop_pending, loop_token>>
TypeOK ==
/\ active \in [Node -> BOOLEAN]
/\ color \in [Node -> Color]
/\ counter \in [Node -> Int]
/\ pending \in [Node -> Nat]
/\ token \in Token
\* @type: Int => Bool;
PassTokenEnabled(i) ==
/\ ~active[i] \* If machine i is active, keep the token.
/\ token.pos = i
\* @type: Bool;
InitiateProbeEnabled ==
/\ token.pos = 0
/\ \* previous round not conclusive if:
\/ token.color = "black"
\/ color[0] = "black"
\/ counter[0] + token.q > 0
\* @type: Bool;
SystemEnabled ==
\/ \E i \in Node \ {0}: PassTokenEnabled(i)
\/ InitiateProbeEnabled
------------------------------------------------------------------------------
BuchiInit ==
buchi_state := 0
AuxInit ==
/\ InLoop \in {TRUE, FALSE}
/\ PrevInLoop = FALSE
/\ loop_vars = vars
/\ loop_fair = (~InLoop \/ ~SystemEnabled)
/\ BuchiInit
/\ loop_buchi_state = buchi_state
/\ buchi_acceptingSeen := (InLoop /\ buchi_state = 1)
Init ==
(* EWD840 but nodes *)
/\ active \in [Node -> BOOLEAN]
/\ color \in [Node -> Color]
(* Rule 0 *)
/\ counter = [i \in Node |-> 0] \* c properly initialized
/\ pending = [i \in Node |-> 0]
/\ token = [pos |-> 0, q |-> 0, color |-> "black"]
/\ AuxInit
InitiateProbe ==
(* Rules 1 + 5 + 6 *)
/\ token.pos = 0
/\ \* previous round not conclusive if:
\/ token.color = "black"
\/ color[0] = "black"
\/ counter[0] + token.q > 0
/\ token' = [pos |-> N-1, q |-> 0, color |-> "white"]
/\ color' = [ color EXCEPT ![0] = "white" ]
\* The state of the nodes remains unchanged by token-related actions.
/\ UNCHANGED <<active, counter, pending>>
PassToken(i) ==
(* Rules 2 + 4 + 7 *)
/\ ~ active[i] \* If machine i is active, keep the token.
/\ token.pos = i
/\ token' = [token EXCEPT !.pos = @ - 1,
!.q = @ + counter[i],
!.color = IF color[i] = "black" THEN "black" ELSE @]
/\ color' = [ color EXCEPT ![i] = "white" ]
\* The state of the nodes remains unchanged by token-related actions.
/\ UNCHANGED <<active, counter, pending>>
System == \/ InitiateProbe
\/ \E i \in Node \ {0} : PassToken(i)
-----------------------------------------------------------------------------
SendMsg(i) ==
\* Only allowed to send msgs if node i is active.
/\ active[i]
(* Rule 0 *)
/\ counter' = [counter EXCEPT ![i] = @ + 1]
\* Non-deterministically choose a receiver node.
/\ \E j \in Node \ {i} : pending' = [pending EXCEPT ![j] = @ + 1]
\* Note that we don't blacken node i as in EWD840 if node i
\* sends a message to node j with j > i
/\ UNCHANGED <<active, color, token>>
RecvMsg(i) ==
/\ pending[i] > 0
/\ pending' = [pending EXCEPT ![i] = @ - 1]
(* Rule 0 *)
/\ counter' = [counter EXCEPT ![i] = @ - 1]
(* Rule 3 *)
/\ color' = [ color EXCEPT ![i] = "black" ]
\* Receipt of a message activates i.
/\ active' = [ active EXCEPT ![i] = TRUE ]
/\ UNCHANGED <<token>>
Deactivate(i) ==
/\ active[i]
/\ active' = [active EXCEPT ![i] = FALSE]
/\ UNCHANGED <<color, counter, pending, token>>
Environment == \E i \in Node : SendMsg(i) \/ RecvMsg(i) \/ Deactivate(i)
Plus(a, b) == a + b
-----------------------------------------------------------------------------
(***************************************************************************)
(* Main safety property: if there is a white token at node 0 and there are *)
(* no in-flight messages then every node is inactive. *)
(***************************************************************************)
terminationDetected ==
/\ token.pos = 0
/\ token.color = "white"
/\ token.q + counter[0] = 0
/\ color[0] = "white"
/\ ~ active[0]
/\ pending[0] = 0
(***************************************************************************)
(* The number of messages on their way. "in-flight" *)
(***************************************************************************)
B == FoldFunction(Plus, 0, pending)
(***************************************************************************)
(* The system has terminated if no node is active and there are no *)
(* in-flight messages. *)
(***************************************************************************)
Termination ==
/\ \A i \in Node : ~ active[i]
/\ B = 0
TerminationDetection ==
terminationDetected => Termination
(**************************************************************************)
LoopNext ==
/\ InLoop' \in {TRUE, FALSE}
/\ loop_active' \in {loop_active, active'}
/\ loop_color' \in {loop_color, color'}
/\ loop_counter' \in {loop_counter, counter'}
/\ loop_pending' \in {loop_pending, pending'}
/\ loop_token' \in {loop_token, token'}
/\ loop_buchi_state' \in {loop_buchi_state, buchi_state'}
/\ (~InLoop /\ InLoop') => (loop_vars' = vars' /\ loop_buchi_state' = buchi_state')
/\ (InLoop = InLoop') => (loop_vars' = loop_vars /\ loop_buchi_state' = loop_buchi_state)
/\ (InLoop) => (InLoop')
/\ PrevInLoop' = InLoop
BuchiNext ==
/\ buchi_state' \in {0,1,-1}
/\ (buchi_state' = 1) => \/ (buchi_state = 0 /\ Termination /\ ~terminationDetected)
\/ (buchi_state = 1 /\ ~terminationDetected)
/\ (buchi_state' = 0) => (buchi_state = 0)
AuxNext ==
/\ loop_fair' := (loop_fair /\ (~InLoop' \/ ~SystemEnabled'))
/\ buchi_acceptingSeen' :=
\/ buchi_acceptingSeen
\/ /\ InLoop'
/\ buchi_state' = 1
Next == /\ BuchiNext
/\ (((System \/ Environment) /\ LoopNext) \/ (UNCHANGED << vars, loop_vars, InLoop, loop_buchi_state >> /\ PrevInLoop' = InLoop))
/\ AuxNext
Spec == Init /\ [][Next]_vars /\ WF_vars(System)
-----------------------------------------------------------------------------
(***************************************************************************)
(* Bound the otherwise infinite state space that TLC has to check. *)
(***************************************************************************)
StateConstraint ==
/\ \A i \in Node : counter[i] <= 3 /\ pending[i] <= 3
/\ token.q <= 9
-----------------------------------------------------------------------------
(***************************************************************************)
(* Safra's inductive invariant *)
(***************************************************************************)
Inv ==
/\ P0:: B = FoldFunction(Plus, 0, counter)
(* (Ai: t < i < N: machine nr.i is passive) /\ *)
(* (Si: t < i < N: ci.i) = q *)
/\ \/ P1:: /\ \A i \in {x \in Node: (token.pos+1) <= x}: ~ active[i] \* machine nr.i is passive
/\ IF token.pos = N-1
THEN token.q = 0
ELSE token.q = FoldFunctionOnSet(Plus, 0, counter, {x \in Node: (token.pos+1) <= x})
(* (Si: 0 <= i <= t: c.i) + q > 0. *)
\/ P2:: FoldFunctionOnSet(Plus, 0, counter, {x \in Node: x <= token.pos}) + token.q > 0
(* Ei: 0 <= i <= t : machine nr.i is black. *)
\/ P3:: \E i \in {x \in Node: x <= token.pos} : color[i] = "black"
(* The token is black. *)
\/ P4:: token.color = "black"
(***************************************************************************)
(* Liveness property: termination is eventually detected. *)
(***************************************************************************)
\* @type: Bool;
LoopOK ==
/\ InLoop /\ PrevInLoop
/\ loop_active = active
/\ loop_color = color
/\ loop_counter = counter
/\ loop_pending = pending
/\ loop_token = token
/\ loop_buchi_state = buchi_state
LoopFair ==
loop_fair
Property ==
LoopOK /\ LoopFair => ~buchi_acceptingSeen
Liveness ==
[](Termination => <>terminationDetected)
(***************************************************************************)
(* The algorithm implements the specification of termination detection *)
(* in a ring with asynchronous communication. *)
(* The parameters of module AsyncTerminationDetection are instantiated *)
(* by the symbols of the same name of the present module. *)
(***************************************************************************)
TD == INSTANCE AsyncTerminationDetection
THEOREM Spec => TD!Spec
=============================================================================
Checked with TLC in 01/2021 with two cores on a fairly modern desktop
and the given state constraint StateConstraint above:
| N | Diameter | Distinct States | States | Time |
| --- | --- | --- | --- | --- |
| 3 | 60 | 1.3m | 10.1m | 42 s |
| 4 | 105 | 219m | 2.3b | 50 m |