-
Notifications
You must be signed in to change notification settings - Fork 2
/
tla-example-with-functor.dl
363 lines (283 loc) · 9.7 KB
/
tla-example-with-functor.dl
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
.type ServerId <: symbol
.type ClientId <: symbol
.functor hashsum(i1:number, i2:number):number stateful
/*
* Ordering predicates (scaffolding)
*/
.decl IsServer(id: ServerId)
.decl ServerLT(s1: ServerId, s2: ServerId)
ServerLT(s1, s2) :-
IsServer(s1),
IsServer(s2),
ord(s1) < ord(s2).
.decl ServerIndex(s: ServerId, index: number)
ServerIndex(s, i) :-
IsServer(s),
i = count : ServerLT(_, s).
.decl FirstServer(s: ServerId)
FirstServer(s) :-
IsServer(s),
!ServerLT(_, s).
.decl LastServer(s: ServerId)
LastServer(s) :-
IsServer(s),
!ServerLT(s, _).
.decl LastServerIndex(s: ServerId, index: number)
LastServerIndex(s, index) :-
LastServer(s),
ServerIndex(s, index).
.decl NextServer(p: ServerId, n: ServerId)
NextServer(p, n) :-
ServerIndex(p, i),
ServerIndex(n, i+1).
.decl NumServers(n: number)
NumServers(n+1) :-
LastServerIndex(_, n).
.decl IsClient(id: ClientId)
.decl ClientLT(c1: ClientId, c2: ClientId)
ClientLT(c1, c2) :-
IsClient(c1),
IsClient(c2),
ord(c1) < ord(c2).
.decl ClientIndex(c: ClientId, index: number)
ClientIndex(c, i) :-
IsClient(c),
i = count : ClientLT(_, c).
.decl FirstClient(c: ClientId)
FirstClient(c) :-
IsClient(c),
!ClientLT(_, c).
.decl LastClient(c: ClientId)
LastClient(c) :-
IsClient(c),
!ClientLT(c, _).
.decl LastClientIndex(c: ClientId, index: number)
LastClientIndex(c, index) :-
LastClient(c),
ClientIndex(c, index).
.decl NextClient(p: ClientId, n: ClientId)
NextClient(p, n) :-
ClientIndex(p, i),
ClientIndex(n, i+1).
.decl NumClients(n: number)
NumClients(n+1) :-
LastClientIndex(_, n).
// Similar ordering for client-server pairs
.decl ClientServerIndex(c: ClientId, s: ServerId, i: number)
ClientServerIndex(c, s, ci*ns + si) :-
ClientIndex(c, ci),
ServerIndex(s, si),
NumServers(ns).
/*
* Types
*/
.type Action = [
kind : symbol, // "connect" or "disconnect"
clientId : ClientId,
serverId : ServerId
]
.type LockedState = [
serverIndex: number,
rest: LockedState
]
.type HeldState = [
clientServerIndex: number,
rest: HeldState
]
.type State = [
locked: LockedState,
held: HeldState
]
.decl DecomposeAction(a: Action, k: symbol, c: ClientId, s: ServerId)
DecomposeAction(a, k, c, s) :-
IsClient(c),
IsServer(s),
(k = "connect"; k = "disconnect"),
a = [k, c, s].
// Single transition component
.comp Transition {
/*
* Main state predicates
*/
.decl Locked(state: State, server: ServerId)
.decl Locked_Prime(prevState: State, action: Action, server: ServerId)
.decl Locked_Prime_Index(prevState: State, action: Action, si: number)
// client holds server
.decl Held(state: State, client: ClientId, server: ServerId)
.decl Held_Prime(prevState: State, action: Action, client: ClientId, server: ServerId)
.decl Held_Prime_Index(prevState: State, action: Action, csi: number)
.decl IsState(state: State)
IsState(state) :-
(Locked(state, _);
Held(state, _, _)),
state != nil.
Locked_Prime_Index(prevState, action, si) :-
Locked_Prime(prevState, action, server),
ServerIndex(server, si).
Held_Prime_Index(prevState, action, csi) :-
Held_Prime(prevState, action, client, server),
ClientServerIndex(client, server, csi).
/*
* Forall iterations, in order to finalize the state. Scaffolding, not essential logic.
*/
.decl PossibleAction(prevState: State, action: Action)
PossibleAction(prevState, action) :-
ConnectPossibleAction(prevState, action);
DisconnectPossibleAction(prevState, action).
.decl HeldStateUpTo(prevState: State, action: Action, clientServerIndex: number, partialState: HeldState)
HeldStateUpTo(prevState, action, index, [index, nil]) :-
PossibleAction(prevState, action),
index = @@hashsum i: 0, {Held_Prime_Index(prevState, action, i)}.
// HeldStateUpTo(prevState, action, nextIndex, [nextIndex, prevPartialState]) :-
// HeldStateUpTo(prevState, action, prevIndex, prevPartialState),
// nextIndex = min(i): {
// Held_Prime_Index(prevState, action, i),
// i > prevIndex
// }.
.decl LockedStateUpTo(prevState: State, action: Action, si: number, partialState: LockedState)
LockedStateUpTo(prevState, action, index, [index, nil]) :-
PossibleAction(prevState, action),
index = @@hashsum i: 0, {Locked_Prime_Index(prevState, action, i)}.
// LockedStateUpTo(prevState, action, nextIndex, [nextIndex, prevPartialState]) :-
// LockedStateUpTo(prevState, action, prevIndex, prevPartialState),
// nextIndex = min(i): {
// Locked_Prime_Index(prevState, action, i),
// i > prevIndex
// }.
.decl HeldStateFinalized(prevState: State, action: Action, hstate: HeldState)
HeldStateFinalized(prevState, action, state) :-
HeldStateUpTo(prevState, action, maxIndex, state),
maxIndex = @@hashsum i: 0, {Held_Prime_Index(prevState, action, i)}.
HeldStateFinalized(prevState, action, nil) :-
PossibleAction(prevState, action),
!Held_Prime_Index(prevState, action, _).
.decl LockedStateFinalized(prevState: State, action: Action, lstate: LockedState)
LockedStateFinalized(prevState, action, state) :-
LockedStateUpTo(prevState, action, maxIndex, state),
maxIndex = @@hashsum i: 0, {Locked_Prime_Index(prevState, action, i)}.
LockedStateFinalized(prevState, action, nil) :-
PossibleAction(prevState, action),
!Locked_Prime_Index(prevState, action, _).
.decl TotalStateFinalized(state: State, prevState: State, action: Action)
TotalStateFinalized([ls, hs], prevState, action) :-
HeldStateFinalized(prevState, action, hs),
LockedStateFinalized(prevState, action, ls).
/*
* Optimization predicates capturing just the "not equals" relation
*/
.decl NotSameServer(s1: ServerId, s2: ServerId)
NotSameServer(s1, s2) :-
IsServer(s1),
IsServer(s2),
s1 != s2.
.decl NotSameClient(c1: ClientId, c2: ClientId)
NotSameClient(c1, c2) :-
IsClient(c1),
IsClient(c2),
c1 != c2.
/*
* Transition rules for next state
*/
// Auxiliary predicates, both for optimization and used elsewhere
.decl ConnectPossible(prevState: State, c: ClientId, s: ServerId, action: Action)
ConnectPossible(prevState, c, s, action) :-
Locked(prevState, s),
DecomposeAction(action, "connect", c, s).
.decl ConnectPossibleAction(prevState: State, action: Action)
ConnectPossibleAction(prevState, action) :-
ConnectPossible(prevState, _, _, action).
// What happens to Held, upon a connect
Held_Prime(prevState, action, c2, s2) :-
ConnectPossibleAction(prevState, action),
Held(prevState, c2, s2).
Held_Prime(prevState, action, c, s) :-
ConnectPossible(prevState, c, s, action).
// Locked, upon a connect
Locked_Prime(prevState, action, s2) :-
ConnectPossible(prevState, _, s, action),
Locked(prevState, s2),
NotSameServer(s, s2).
// Optimization predicates
.decl DisconnectPossible(prevState: State, c: ClientId, s: ServerId, action: Action)
DisconnectPossible(prevState, c, s, action) :-
Held(prevState, c, s),
DecomposeAction(action, "disconnect", c, s).
.decl DisconnectPossibleAction(prevState: State, action: Action)
DisconnectPossibleAction(prevState, action) :-
DisconnectPossible(prevState, _, _, action).
// What happens to Held, upon a disconnect
Held_Prime(prevState, action, c2, s2) :-
DisconnectPossible(prevState, c, s, action),
Held(prevState, c2, s2),
(NotSameClient(c, c2);
NotSameServer(s, s2)).
// Locked, upon a disconnect
Locked_Prime(prevState, action, s2) :-
DisconnectPossibleAction(prevState, action),
Locked(prevState, s2).
Locked_Prime(prevState, action, s) :-
DisconnectPossible(prevState, _, s, action).
.output IsState
} // end of Transition component
.init T1 = Transition
.init T2 = Transition
.init T3 = Transition
.init T4 = Transition
.init T5 = Transition
.init T6 = Transition
.init T7 = Transition
.init TFinal = Transition
/*
* Initial state (successor of nil)
*/
// Initial state is nil
T1.Locked(nil, s) :-
IsServer(s).
/*
* Connecting transitions
*/
#define CopyRelations(tNew, tOld) \
tNew.Held(state, client, server) :- tOld.Held(state, client, server). \
tNew.Locked(state, server) :- tOld.Locked(state, server). \
tNew.Held(state, client, server) :- tOld.TotalStateFinalized(state, prevState, action), tOld.Held_Prime(prevState, action, client, server). \
tNew.Locked(state, server) :- tOld.TotalStateFinalized(state, prevState, action), tOld.Locked_Prime(prevState, action, server).
// tNew.Held_Prime(state, action, client, server) :- tOld.Held_Prime(state, action, client, server). \
// tNew.Held_Prime_Index(state, action, csi) :- tOld.Held_Prime_Index(state, action, csi). \
// tNew.Locked_Prime(state, action, server) :- tOld.Locked_Prime(state, action, server). \
// tNew.Locked_Prime_Index(state, action, si) :- tOld.Locked_Prime_Index(state, action, si). \
// tNew.HeldStateUpTo(prevState, action, clientServerIndex, partialState) :- tOld.HeldStateUpTo(prevState, action, clientServerIndex, partialState). \
// tNew.LockedStateUpTo(prevState, action, si, partialState) :- tOld.LockedStateUpTo(prevState, action, si, partialState). \
CopyRelations(T2, T1)
CopyRelations(T3, T2)
CopyRelations(T4, T3)
CopyRelations(T5, T4)
CopyRelations(T6, T5)
CopyRelations(T7, T6)
CopyRelations(TFinal, T7)
.decl Unsafe(state: State, c1: ClientId, c2: ClientId)
Unsafe(state, c1, c2) :-
TFinal.Held(state, c1, s),
TFinal.Held(state, c2, s),
c1 != c2.
/*
* Setup of problem instance
*/
IsServer("a").
IsServer("b").
IsServer("c").
IsServer("d").
IsServer("e").
IsServer("f").
IsServer("g").
IsClient("1").
IsClient("2").
IsClient("3").
IsClient("4").
IsClient("5").
IsClient("6").
IsClient("7").
// .printsize Locked
// .printsize Held
.printsize TFinal.IsState
// .output IsState
.output Unsafe