-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdenotational.c
More file actions
406 lines (358 loc) · 12.1 KB
/
denotational.c
File metadata and controls
406 lines (358 loc) · 12.1 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* -*- coding: utf-8 -*-
* -----------------------------------------------------------------------------
* Copyright © 2016-2017, HST Project.
* Please see the COPYING file in this distribution for license details.
* -----------------------------------------------------------------------------
*/
#include "denotational.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "ccan/container_of/container_of.h"
#include "environment.h"
#include "event.h"
#include "macros.h"
#include "normalization.h"
#include "operators.h"
#include "process.h"
#include "refinement.h"
/*------------------------------------------------------------------------------
* Traces
*/
#define CSP_EMPTY_TRACE_HASH UINT64_C(0x0e40beb4b610bc78) /* random */
struct csp_trace
csp_trace_init(const struct csp_event *event, struct csp_trace *prev)
{
struct csp_trace trace = {event, prev, prev->hash ^ csp_event_id(event)};
return trace;
}
struct csp_trace
csp_trace_init_empty(void)
{
struct csp_trace trace = {NULL, NULL, CSP_EMPTY_TRACE_HASH};
return trace;
}
struct csp_trace *
csp_trace_new(const struct csp_event *event, struct csp_trace *prev)
{
struct csp_trace *trace = malloc(sizeof(struct csp_trace));
assert(trace != NULL);
*trace = csp_trace_init(event, prev);
return trace;
}
struct csp_trace *
csp_trace_new_empty(void)
{
struct csp_trace *trace = malloc(sizeof(struct csp_trace));
assert(trace != NULL);
*trace = csp_trace_init_empty();
return trace;
}
void
csp_trace_free(struct csp_trace *trace)
{
free(trace);
}
void
csp_trace_free_deep(struct csp_trace *trace)
{
if (trace != NULL) {
struct csp_trace *prev = trace->prev;
free(trace);
csp_trace_free_deep(prev);
}
}
bool
csp_trace_empty(const struct csp_trace *trace)
{
return trace->event == NULL;
}
bool
csp_trace_eq(const struct csp_trace *trace1, const struct csp_trace *trace2)
{
if (csp_trace_empty(trace1) || csp_trace_empty(trace2)) {
return csp_trace_empty(trace1) == csp_trace_empty(trace2);
}
if (trace1->event != trace2->event) {
return false;
}
return csp_trace_eq(trace1->prev, trace2->prev);
}
struct csp_trace_print {
struct csp_trace_visitor visitor;
struct csp_name_visitor *wrapped;
bool first;
};
static void
csp_trace_print_visit(struct csp *csp, struct csp_trace_visitor *visitor,
const struct csp_trace *trace)
{
struct csp_trace_print *self =
container_of(visitor, struct csp_trace_print, visitor);
if (csp_trace_empty(trace)) {
return;
}
if (self->first) {
self->first = false;
} else {
csp_name_visitor_call(csp, self->wrapped, ",");
}
csp_name_visitor_call(csp, self->wrapped, csp_event_name(trace->event));
}
void
csp_trace_print(struct csp *csp, const struct csp_trace *trace,
struct csp_name_visitor *visitor)
{
struct csp_trace_print self = {{csp_trace_print_visit}, visitor, true};
csp_name_visitor_call(csp, visitor, "⟨");
csp_trace_visit_prefixes(csp, trace, &self.visitor);
csp_name_visitor_call(csp, visitor, "⟩");
}
struct csp_process *
csp_process_from_trace(struct csp *csp, const struct csp_trace *trace)
{
struct csp_process *current = csp->stop;
while (!csp_trace_empty(trace)) {
current = csp_prefix(csp, trace->event, current);
trace = trace->prev;
}
return current;
}
bool
csp_process_has_trace(struct csp *csp, struct csp_process *process,
const struct csp_trace *trace)
{
assert(trace != NULL);
struct csp_process *trace_process = csp_process_from_trace(csp, trace);
return csp_check_traces_refinement(csp, process, trace_process);
}
/*------------------------------------------------------------------------------
* Trace element visitor
*/
void
csp_trace_visitor_call(struct csp *csp, struct csp_trace_visitor *visitor,
const struct csp_trace *trace)
{
visitor->visit(csp, visitor, trace);
}
static void
csp_print_traces_visit(struct csp *csp, struct csp_trace_visitor *visitor,
const struct csp_trace *trace)
{
struct csp_print_traces *self =
container_of(visitor, struct csp_print_traces, visitor);
csp_trace_print(csp, trace, self->wrapped);
csp_name_visitor_call(csp, self->wrapped, "\n");
}
struct csp_print_traces
csp_print_traces(struct csp_name_visitor *wrapped)
{
struct csp_print_traces self = {{csp_print_traces_visit}, wrapped};
return self;
}
static void
csp_trace_visit_one_prefix(struct csp *csp, const struct csp_trace *trace,
struct csp_trace_visitor *visitor)
{
if (!csp_trace_empty(trace)) {
csp_trace_visit_one_prefix(csp, trace->prev, visitor);
}
csp_trace_visitor_call(csp, visitor, trace);
}
void
csp_trace_visit_prefixes(struct csp *csp, const struct csp_trace *trace,
struct csp_trace_visitor *visitor)
{
csp_trace_visit_one_prefix(csp, trace, visitor);
}
/*------------------------------------------------------------------------------
* Finite traces
*/
/* The prenormalization code can do most of the work for us; it will give us a
* bunch of subprocesses with at most one outgoing transition for any event. We
* then just have to walk through its edges. */
struct csp_subprocess_traces {
struct csp_edge_visitor visitor;
struct csp_trace_visitor *wrapped;
struct csp_subprocess_traces *prev;
struct csp_process *process;
struct csp_trace trace;
bool any_afters;
};
static bool
csp_subprocess_traces_contains_process(struct csp_subprocess_traces *self,
struct csp_process *process)
{
if (self == NULL) {
return false;
}
if (self->process == process) {
return true;
}
return csp_subprocess_traces_contains_process(self->prev, process);
}
static void
csp_subprocess_traces_visit(struct csp *csp, struct csp_edge_visitor *visitor,
const struct csp_event *initial,
struct csp_process *after)
{
struct csp_subprocess_traces *self =
container_of(visitor, struct csp_subprocess_traces, visitor);
struct csp_subprocess_traces next;
self->any_afters = true;
next.visitor.visit = csp_subprocess_traces_visit;
next.wrapped = self->wrapped;
next.prev = self;
next.process = after;
next.trace = csp_trace_init(initial, &self->trace);
next.any_afters = false;
/* If the current trace already contains `after`, then we've encountered a
* cycle, and have encountered a maximal finite trace. */
if (csp_subprocess_traces_contains_process(self, after)) {
csp_trace_visitor_call(csp, self->wrapped, &next.trace);
} else {
csp_process_visit_transitions(csp, after, &next.visitor);
if (!next.any_afters) {
csp_trace_visitor_call(csp, self->wrapped, &next.trace);
}
}
}
void
csp_process_visit_maximal_finite_traces(struct csp *csp,
struct csp_process *process,
struct csp_trace_visitor *wrapped)
{
struct csp_process *prenormalized = csp_prenormalize_process(csp, process);
struct csp_subprocess_traces start = {
{csp_subprocess_traces_visit}, wrapped, NULL, prenormalized,
csp_trace_init_empty(), false};
csp_process_visit_transitions(csp, prenormalized, &start.visitor);
if (!start.any_afters) {
csp_trace_visitor_call(csp, wrapped, &start.trace);
}
}
/*------------------------------------------------------------------------------
* Traced process
*/
struct csp_traced_process {
struct csp_process process;
struct csp_process *wrapped;
struct csp_trace trace;
};
static void
csp_traced_process_name(struct csp *csp, struct csp_process *process,
struct csp_name_visitor *visitor)
{
struct csp_traced_process *traced =
container_of(process, struct csp_traced_process, process);
csp_trace_print(csp, &traced->trace, visitor);
csp_name_visitor_call(csp, visitor, " ⇒ ");
csp_process_name(csp, traced->wrapped, visitor);
}
static void
csp_traced_process_initials(struct csp *csp, struct csp_process *process,
struct csp_event_visitor *visitor)
{
struct csp_traced_process *traced =
container_of(process, struct csp_traced_process, process);
csp_process_visit_initials(csp, traced->wrapped, visitor);
}
struct csp_traced_process_after {
struct csp_edge_visitor visitor;
const struct csp_trace *trace;
struct csp_edge_visitor *wrapped;
};
static void
csp_traced_process_after_visit(struct csp *csp,
struct csp_edge_visitor *visitor,
const struct csp_event *initial,
struct csp_process *wrapped_after)
{
struct csp_traced_process_after *traced =
container_of(visitor, struct csp_traced_process_after, visitor);
struct csp_process *after =
csp_traced_process_new(csp, wrapped_after, traced->trace);
csp_edge_visitor_call(csp, traced->wrapped, initial, after);
}
static struct csp_traced_process_after
csp_traced_process_after(const struct csp_trace *trace,
struct csp_edge_visitor *wrapped)
{
struct csp_traced_process_after self = {
{csp_traced_process_after_visit}, trace, wrapped};
return self;
}
static void
csp_traced_process_afters(struct csp *csp, struct csp_process *process,
const struct csp_event *initial,
struct csp_edge_visitor *visitor)
{
struct csp_traced_process *traced =
container_of(process, struct csp_traced_process, process);
struct csp_trace trace;
struct csp_traced_process_after after;
trace = csp_trace_init(initial, &traced->trace);
after = csp_traced_process_after(&trace, visitor);
csp_process_visit_afters(csp, traced->wrapped, initial, &after.visitor);
}
static void
csp_traced_process_free(struct csp *csp, struct csp_process *process)
{
struct csp_traced_process *traced =
container_of(process, struct csp_traced_process, process);
free(traced);
}
static const struct csp_process_iface csp_traced_process_iface = {
0, csp_traced_process_name, csp_traced_process_initials,
csp_traced_process_afters, csp_traced_process_free};
static csp_id
csp_traced_process_get_id(struct csp_process *wrapped,
const struct csp_trace *trace)
{
static struct csp_id_scope traced_process;
csp_id id = csp_id_start(&traced_process);
id = csp_id_add_process(id, wrapped);
id = csp_id_add_id(id, trace->hash);
return id;
}
struct csp_process *
csp_traced_process_new(struct csp *csp, struct csp_process *wrapped,
const struct csp_trace *trace)
{
csp_id id = csp_traced_process_get_id(wrapped, trace);
struct csp_traced_process *traced;
return_if_nonnull(csp_get_process(csp, id));
traced = malloc(sizeof(struct csp_traced_process));
assert(traced != NULL);
traced->process.id = id;
traced->process.iface = &csp_traced_process_iface;
traced->wrapped = wrapped;
traced->trace = *trace;
csp_register_process(csp, &traced->process);
return &traced->process;
}
static struct csp_traced_process *
csp_traced_process_downcast(struct csp_process *process)
{
assert(process->iface == &csp_traced_process_iface);
return container_of(process, struct csp_traced_process, process);
}
struct csp_process *
csp_traced_process(struct csp *csp, struct csp_process *wrapped)
{
struct csp_trace trace = csp_trace_init_empty();
return csp_traced_process_new(csp, wrapped, &trace);
}
const struct csp_trace *
csp_traced_process_get_trace(struct csp *csp, struct csp_process *process)
{
struct csp_traced_process *traced = csp_traced_process_downcast(process);
return &traced->trace;
}
struct csp_process *
csp_traced_process_get_wrapped(struct csp *csp, struct csp_process *process)
{
struct csp_traced_process *traced = csp_traced_process_downcast(process);
return traced->wrapped;
}