-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchoreo.qnt
More file actions
292 lines (255 loc) · 9.19 KB
/
choreo.qnt
File metadata and controls
292 lines (255 loc) · 9.19 KB
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
// -*- mode: Bluespec; -*-
/**
* Choreo: Choreograph distributed protocols in Quint
*
* Read the documentation: TODO LINK
*
* Gabriela Moreira, Josef Widder and Yassine Boukhari,
* Informal Systems, 2025
*/
module choreo {
import basicSpells.* from "spells/basicSpells"
// TODO: try moving process id to local context
type LocalState[process_id, ext] = {
process_id: process_id
| ext
}
type Transition[p, s, m, e, ce] = {
post_state: LocalState[p, s],
effects: Set[Effect[p, m, e, ce]],
}
type GlobalContext[p, s, m, e, ext] = {
system: p -> LocalState[p, s],
messages: p -> Set[m],
events: p -> Set[e],
extensions: ext
}
type LocalContext[p, s, m, e, ext] = {
state: LocalState[p, s],
messages: Set[m],
events: Set[e],
extensions: ext
}
// This is the message routing to the # handler
type Listener[p, s, m, e, ce, ext] =
(LocalContext[p, s, m, e, ext]) => Set[Transition[p, s, m, e, ce]]
type DeterministicListener[p, s, m, e, ce, ext] =
(LocalContext[p, s, m, e, ext]) => Transition[p, s, m, e, ce]
// Only needed for micro_step
type Input[m, e] = Message(m) | Event(e)
// Only needed for micro_step
type MicroListener[p, s, m, e, ce, ext] =
(LocalContext[p, s, m, e, ext], Input[m, e]) => Set[Transition[p, s, m, e, ce]]
type EffectProcessor[p, s, m, e, ce, ext] =
(GlobalContext[p, s, m, e, ext], ce) => GlobalContext[p, s, m, e, ext]
type Effect[p, m, e, ce] =
| Broadcast(m)
| Send({ to: p, message: m })
| TriggerEvent(e)
| CustomEffect(ce)
/// A displayer is a function that takes a global context and returns a displayable representation of it.
/// This is used to visualize the state of the system for debugging or monitoring purposes.
/// After type instantiation, it should have the following signature:
/// ```
/// (Environment) => Display
/// ```
/// The display type can be anything.
type Displayer[p, s, m, e, ext, d] = (GlobalContext[p, s, m, e, ext]) => d
pure def apply_effect(
env: GlobalContext[p, s, m, e, ext],
v: p,
tr: Transition[p, s, m, e, ce],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, ext]
): GlobalContext[p, s, m, e, ext] = {
val env1 = { ...env, system: env.system.setBy(v, s => tr.post_state) }
tr.effects.fold(env1, (e, effect) => {
match effect {
| Broadcast(m) => {
{ ...e, messages: e.messages.transformValues(b => b.setAdd(m)) }
}
| Send(r) => {
{ ...e, messages: e.messages.setBy(r.to, b => b.setAdd(r.message)) }
}
| TriggerEvent(ev) => {
{ ...e, events: e.events.setBy(v, b => b.setAdd(ev)) }
}
| CustomEffect(ex) => {
apply_custom_effect(e, ex)
}
}
})
}
const processes: Set[p]
var s: GlobalContext[p, s, m, e, ext]
var display: d
pure def initialize(x: a, f: Option[(a) => Set[b]]): Set[b] = {
match f {
| Some(fun) => fun(x)
| None => Set()
}
}
pure def convert_context(
env: GlobalContext[p, s, m, e, ext],
v: p
): LocalContext[p, s, m, e, ext] = {
{
state: env.system.get(v),
messages: env.messages.get(v),
events: env.events.get(v),
extensions: env.extensions
}
}
action process_transitions(
v: p,
transitions: Set[Transition[p, s, m, e, ce]],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions],
): bool =
nondet transition = oneOf(transitions)
val post_env = apply_effect(s, v, transition, apply_custom_effect)
s' = post_env
action process_transitions_with_displayer(
v: p,
transitions: Set[Transition[p, s, m, e, ce]],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions],
displayer: Displayer[p, s, m, e, extensions, d],
): bool =
nondet transition = oneOf(transitions)
val post_env = apply_effect(s, v, transition, apply_custom_effect)
all {
s' = post_env,
display' = displayer(post_env),
}
action init(ctx: GlobalContext[p, s, m , e, ext]): bool = {
s' = ctx
}
action init_with_displayer(
ctx: GlobalContext[p, s, m , e, ext],
displayer: Displayer[p, s, m, e, extensions, d],
): bool = all {
s' = ctx,
display' = displayer(ctx)
}
action step(
listener: Listener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions]
): bool = {
nondet v = oneOf(processes)
val input = convert_context(s, v)
val transitions = listener(input).filter(t => t.effects.size() > 0 or t.post_state != input.state)
process_transitions(v, transitions, apply_custom_effect)
}
action step_with_displayer(
listener: Listener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions],
displayer: Displayer[p, s, m, e, extensions, d]
): bool = {
nondet v = oneOf(processes)
val input = convert_context(s, v)
val transitions = listener(input).filter(t => t.effects.size() > 0 or t.post_state != input.state)
process_transitions_with_displayer(v, transitions, apply_custom_effect, displayer)
}
action micro_step(
listener: MicroListener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions]
): bool = {
nondet process = processes.oneOf()
val ctx = convert_context(s, process)
any {
nondet msg = s.messages.get(process).oneOf()
val transitions = listener(ctx, Message(msg))
process_transitions(process, transitions, apply_custom_effect),
nondet event = s.events.get(process).oneOf()
val transitions = listener(ctx, Event(event))
process_transitions(process, transitions, apply_custom_effect),
}
}
action step_with(
v: p,
listener: Listener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions]
): bool = {
val input = convert_context(s, v)
val transitions = listener(input).filter(t => t.effects.size() > 0 or t.post_state != input.state)
process_transitions(v, transitions, apply_custom_effect)
}
action step_deterministic(
v: p,
listener: DeterministicListener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions]
): bool = {
val input = convert_context(s, v)
val transitions = Set(listener(input)).filter(t => t.effects.size() > 0 or t.post_state != input.state)
process_transitions(v, transitions, apply_custom_effect)
}
action step_with_filter(
v: p,
listener: Listener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions],
f: (Transition[p, s, m, e, ce]) => bool
): bool = {
val input = convert_context(s, v)
val transitions = listener(input).filter(t => t.effects.size() > 0 or t.post_state != input.state).filter(f)
process_transitions(v, transitions, apply_custom_effect)
}
action step_with_messages(
v: p,
listener: Listener[p, s, m, e, ce, extensions],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, extensions],
f: Set[m] => Set[m]
): bool = {
val input = convert_context(s, v)
val input1 = { ...input, messages: f(input.messages) }
val transitions = listener(input1).filter(t => t.effects.size() > 0 or t.post_state != input.state)
process_transitions(v, transitions, apply_custom_effect)
}
action lose_messages(v: p, f: Set[m] => Set[m]): bool = {
val msgs = s.messages.get(v)
val new_msgs = msgs.exclude(f(msgs))
all {
s' = { ...s, messages: s.messages.set(v, new_msgs) }
}
}
pure def cue(
ctx: LocalContext[p, s, m, e, ext],
listen_fn: (LocalContext[p, s, m, e, ext]) => Set[r],
upon_fn: (LocalContext[p, s, m, e, ext], r) => Transition[p, s, m, e, ce]
): Set[Transition[p, s, m, e, ce]] = {
val params = listen_fn(ctx)
params.map(param => upon_fn(ctx, param))
}
type CueResult[p, s, m, e, ext, r] = CueOk({ ctx: LocalContext[p, s, m, e, ext], params: r }) | NoCue
def with_cue(
process: p,
listen_fn: (LocalContext[p, s, m, e, ext]) => Set[r],
params: r
): CueResult[p, s, m, e, ext, r] = {
val ctx = convert_context(s, process)
val valid_params = listen_fn(ctx)
val is_valid = valid_params.contains(params)
if (is_valid)
CueOk({ ctx: ctx, params: params })
else
NoCue
}
action perform(
cue_result: CueResult[p, s, m, e, ext, r],
upon_fn: (LocalContext[p, s, m, e, ext], r) => Transition[p, s, m, e, ce],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, ext],
): bool = {
match cue_result {
| CueOk(cue) => process_transitions(cue.ctx.state.process_id, Set(upon_fn(cue.ctx, cue.params)), apply_custom_effect)
| NoCue => all { false, s' = s }
}
}
action perform_with_displayer(
cue_result: CueResult[p, s, m, e, ext, r],
upon_fn: (LocalContext[p, s, m, e, ext], r) => Transition[p, s, m, e, ce],
apply_custom_effect: EffectProcessor[p, s, m, e, ce, ext],
displayer: Displayer[p, s, m, e, extensions, d],
): bool = {
match cue_result {
| CueOk(cue) => process_transitions_with_displayer(cue.ctx.state.process_id, Set(upon_fn(cue.ctx, cue.params)), apply_custom_effect, displayer)
| NoCue => all { false, s' = s, display' = display }
}
}
}