forked from epfl-lara/stainless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actors.scala
192 lines (147 loc) · 5.1 KB
/
Actors.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
// Disabled as it requires having Akka on the classpath.
object Actors {
def disabled = assert(true)
}
// import stainless.lang._
// import stainless.collection._
// import stainless.annotation._
// object actors {
// abstract class Msg
// sealed abstract class Behavior {
// def processMsg(msg: Msg)(implicit ctx: ActorContext): Behavior
// }
// case class ActorRef(
// name: String,
// @extern @pure
// underlying: akka.actor.ActorRef
// ) {
// @inline
// def !(msg: Msg)(implicit ctx: ActorContext): Unit = {
// ctx.send(this, msg)
// }
// }
// case class ActorContext(
// self: ActorRef,
// @ghost
// var toSend: List[(ActorRef, Msg)] = Nil()
// ) {
// def send(to: ActorRef, msg: Msg): Unit = {
// toSend = (to, msg) :: toSend
// sendUnderlying(to, msg)
// }
// @extern @pure
// private def sendUnderlying(to: ActorRef, msg: Msg): Unit = {
// to.underlying ! msg
// }
// }
// @ghost
// case class ActorSystem(
// behaviors: CMap[ActorRef, Behavior],
// inboxes: CMap[(ActorRef, ActorRef), List[Msg]]
// ) {
// def step(from: ActorRef, to: ActorRef): ActorSystem = {
// inboxes(from -> to) match {
// case Nil() =>
// this
// case Cons(msg, msgs) =>
// val (newBehavior, toSend) = deliverMessage(to, msg)
// val newBehaviors = behaviors.updated(to, newBehavior)
// val newInboxes = toSend.foldLeft(inboxes.updated(from -> to, msgs)) {
// case (acc, (dest, m)) => acc.updated(to -> dest, m :: acc(to -> dest))
// }
// ActorSystem(newBehaviors, newInboxes)
// }
// }
// def deliverMessage(actor: ActorRef, msg: Msg): (Behavior, List[(ActorRef, Msg)]) = {
// val behavior = behaviors(actor)
// val ctx = ActorContext(actor, Nil())
// val nextBehavior = behavior.processMsg(msg)(ctx)
// (nextBehavior, ctx.toSend)
// }
// }
// @ignore
// class ActorWrapper(initialBehavior: Behavior) extends akka.actor.Actor with akka.actor.ActorLogging {
// private def handler(behavior: Behavior): PartialFunction[Any, Unit] = {
// case msg: Msg =>
// val me = ActorRef(context.self.path.name, context.self)
// val ctx = ActorContext(me, Nil())
// val newBehavior = behavior.processMsg(msg)(ctx)
// log.info(s"Received: $msg, becoming $newBehavior")
// context.become(handler(newBehavior), discardOld = true)
// }
// def receive = handler(initialBehavior)
// }
// case class PrimBehav(backup: ActorRef, counter: Counter) extends Behavior {
// require(backup.name == "backup")
// override
// def processMsg(msg: Msg)(implicit ctx: ActorContext): Behavior = msg match {
// case Inc() =>
// backup ! Inc()
// PrimBehav(backup, counter.increment)
// case _ => this
// }
// }
// case class BackBehav(counter: Counter) extends Behavior {
// override
// def processMsg(msg: Msg)(implicit ctx: ActorContext): Behavior = msg match {
// case Inc() =>
// BackBehav(counter.increment)
// case _ => this
// }
// }
// @extern
// def noSender = akka.actor.ActorRef.noSender
// val Primary = ActorRef("primary", noSender)
// val Backup = ActorRef("backup", noSender)
// case class Inc() extends Msg
// case class Counter(value: BigInt) {
// require(value >= 0)
// def increment: Counter = Counter(value + 1)
// def <=(that: Counter): Boolean = this.value <= that.value
// }
// @ghost
// def invariant(s: ActorSystem): Boolean = {
// s.inboxes(Primary -> Primary).isEmpty &&
// s.inboxes(Backup -> Primary).isEmpty &&
// s.inboxes(Backup -> Backup).isEmpty &&
// s.inboxes(Primary -> Backup).forall(_ == Inc()) && {
// (s.behaviors(Primary), s.behaviors(Backup)) match {
// case (PrimBehav(_, p), BackBehav(b)) =>
// p.value == b.value + s.inboxes(Primary -> Backup).length
// case _ => false
// }
// }
// }
// def validRef(ref: ActorRef): Boolean = ref == Primary || ref == Backup
// @ghost
// def theorem(s: ActorSystem, from: ActorRef, to: ActorRef): Boolean = {
// require(invariant(s) && validRef(from) && validRef(to))
// val newSystem = s.step(from, to)
// invariant(newSystem)
// }.holds
// @ignore
// def main(args: Array[String]): Unit = {
// val initCounter = Counter(0)
// val system = akka.actor.ActorSystem("Counter")
// val backupRef = ActorRef(
// "backup",
// system.actorOf(
// akka.actor.Props(new ActorWrapper(BackBehav(initCounter))),
// name = "backup"
// )
// )
// val primaryRef = ActorRef(
// "primary",
// system.actorOf(
// akka.actor.Props(new ActorWrapper(PrimBehav(backupRef, initCounter))),
// name = "primary"
// )
// )
// implicit val ctx = ActorContext(primaryRef, Nil())
// import system.dispatcher
// import scala.concurrent.duration._
// system.scheduler.schedule(500.millis, 1000.millis) {
// primaryRef ! Inc()
// }
// }
// }