-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountingProof.scala
259 lines (204 loc) · 7.22 KB
/
CountingProof.scala
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
package distribution
import FifoNetwork._
import Networking._
import stainless.lang._
import stainless.collection._
import stainless.proof._
import stainless.annotation._
import scala.language.postfixOps
// This object contains lemma and auxiliary functions used in the proofs
object ProtocolProof {
import Protocol._
def validId(net: VerifiedNetwork, id: ActorId) = {
true
}
def listToMap[A,B](l: List[(A,B)]): Map[A,B] = l match {
case Nil() => Map()
case Cons((a,b), ls) => listToMap(ls).updated(a,b)
}
def makeNetwork(p: Parameter) = {
val net = VerifiedNetwork(
NoParam(),
listToMap(List[(ActorId,State)](
(ActorId1(), CCounter(0)),
(ActorId2(), VCounter(0))
)),
listToMap(List()),
listToMap(List[(ActorId,Actor)](
(ActorId1(), CountingActor(actor1)),
(ActorId2(), CheckingActor(actor2))
))
)
CountingActor(actor1).init()(net)
net
} ensuring(res => networkInvariant(res.param, res.states, res.messages, res.getActor))
def runActorsPrecondition(p: Parameter, schedule: List[(ActorId,ActorId,Message)]): Boolean = true
// This is an invariant of the class VerifiedNetwork
def networkInvariant(param: Parameter, states: Map[ActorId, State], messages: Map[(ActorId,ActorId),List[Message]], getActor: Map[ActorId,Actor]) = {
states.contains(actor1) &&
states.contains(actor2) &&
states(actor1) != BadState() &&
states(actor2) != BadState() &&
getActor.contains(actor1) &&
getActor.contains(actor2) &&
getActor(actor1) == CountingActor(actor1) &&
getActor(actor2) == CheckingActor(actor2) &&
!messages.contains((actor2,actor2)) &&
!messages.contains((actor2,actor1)) &&
areIncrements(messages.getOrElse((actor1,actor1), Nil())) &&
((states(actor1),states(actor2)) match {
case (CCounter(i),VCounter(k)) =>
val sms = messages.getOrElse((actor1,actor2), Nil())
areDelivers(sms) &&
isSorted(sms) &&
i >= k && (
sms.isEmpty || (
i >= max(sms) &&
k < min(sms)
)
)
case _ => false
})
}
def countingActorReceivePre(receiver: Actor, sender: ActorId, m: Message)(implicit net: VerifiedNetwork) = {
require(networkInvariant(net.param, net.states, net.messages, net.getActor) && receiver.myId == actor1)
(sender, m, receiver.state) match {
case (ActorId1(), Increment(), CCounter(i)) =>
val j = i+1
val a1a2messages = net.messages.getOrElse((actor1,actor2),Nil())
val a1a1messages = net.messages.getOrElse((actor1,actor1),Nil())
val newStates = net.states.updated(actor1, CCounter(j))
val newa1a2messages = a1a2messages :+ Deliver(j)
val newa1a1messages = a1a1messages :+ Increment()
val newMapMessages = net.messages.updated((actor1,actor2), newa1a2messages)
val newnewMapMessages = newMapMessages.updated((actor1,actor1), newa1a1messages)
val VCounter(k) = net getState(actor2)
areDelivers(a1a2messages) &&
appendDeliver(a1a2messages, j) &&
areIncrements(a1a1messages) &&
appendIncrement(a1a1messages) &&
areIncrements(newa1a1messages) &&
areDelivers(newa1a2messages) &&
appendDeliver(a1a2messages, j) &&
appendItself(a1a2messages, j) &&
j >= max(newa1a2messages) &&
k < j &&
appendLarger(a1a2messages, j, k) &&
k < min(newa1a2messages) &&
isSorted(a1a2messages) &&
appendSorted(a1a2messages, j) &&
isSorted(newa1a2messages) &&
networkInvariant(net.param, newStates, newMapMessages, net.getActor) &&
networkInvariant(net.param, newStates, newnewMapMessages, net.getActor)
case _ => false
}
}
def checkingActorReceivePre(receiver: Actor, sender: ActorId, m: Message)(implicit net: VerifiedNetwork) = {
require(networkInvariant(net.param, net.states, net.messages, net.getActor))
sender == ActorId1() &&
((net getState(actor1), m, net getState(actor2)) match {
case (CCounter(i), Deliver(j),VCounter(k)) =>
val a1a2messages = net.messages.getOrElse((actor1,actor2),Nil())
i >= j && j > k &&
( a1a2messages.isEmpty || j < min(a1a2messages) )
case _ => false
})
}
def peekMessageEnsuresReceivePre(n: VerifiedNetwork, sender: ActorId, receiver: ActorId, m: Message) = {
require(networkInvariant(n.param, n.states, n.messages, n.getActor))
val sms = n.messages.getOrElse((sender, receiver), Nil())
sms match {
case Cons(x, xs) if (x == m) =>
val messages2 = n.messages.updated((sender, receiver), xs)
(sender, receiver) match {
case (ActorId1(), ActorId2()) =>
((n.getState(actor1), m, n.getState(actor2)) match {
case (CCounter(i), Deliver(j),VCounter(k)) =>
val mm = messages2.getOrElse((actor1,actor2),Nil())
smallestHead(j,sms) && (mm.isEmpty || j < min(mm))
})
case (ActorId1(), ActorId1()) =>
true
case _ =>
false
}
case _ =>
true
}
}
def min(l: List[Message]): BigInt = {
require(!l.isEmpty && areDelivers(l))
l match {
case Cons(Deliver(i), Nil()) => i
case Cons(Deliver(i), xs) =>
val m = min(xs)
if (i < m) i else m
}
}
def max(l: List[Message]): BigInt = {
require(!l.isEmpty && areDelivers(l))
l match {
case Cons(Deliver(i), Nil()) => i
case Cons(Deliver(i), xs) =>
val m = max(xs)
if (i > m) i else m
}
}
def isSorted(l: List[Message]): Boolean = {
require(areDelivers(l))
l match {
case Nil() => true
case Cons(x, Nil()) => true
case Cons(Deliver(x), ls@Cons(Deliver(y), ys)) => x < y && isSorted(ls)
}
}
def areDelivers(l: List[Message]): Boolean = {
l match {
case Nil() => true
case Cons(Deliver(_), xs) => areDelivers(xs)
case _ => false
}
}
@induct
def appendDeliver(messages: List[Message], x: BigInt) = {
require(areDelivers(messages))
areDelivers(messages :+ Deliver(x))
} holds
def areIncrements(l: List[Message]): Boolean = {
l match {
case Nil() => true
case Cons(Increment(), xs) => areIncrements(xs)
case _ => false
}
}
@induct
def appendIncrement(messages: List[Message]) = {
require(areIncrements(messages))
areIncrements(messages :+ Increment())
} holds
@induct
def appendItself(l: List[Message], j: BigInt) = {
require(areDelivers(l) && (l.isEmpty || j >= max(l)))
appendDeliver(l,j) &&
j >= max(l :+ Deliver(j))
} holds
@induct
def appendLarger(l: List[Message], j: BigInt, k: BigInt) = {
require(areDelivers(l) && (l.isEmpty || k < min(l)) && k < j)
appendDeliver(l,j) &&
k < min(l :+ Deliver(j))
} holds
@induct
def appendSorted(l: List[Message], j: BigInt) = {
require(l.isEmpty || (areDelivers(l) && isSorted(l) && j > max(l)))
appendDeliver(l,j) &&
isSorted(l :+ Deliver(j))
} holds
@induct
def smallestHead(j: BigInt, l: List[Message]) = {
require(areDelivers(l) && !l.isEmpty && isSorted(l))
l match {
case Cons(Deliver(j), xs) => xs.isEmpty || j < min(xs)
}
} holds
}