@toiroakr/chisel 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +354 -21
- package/dist/absence.d.ts +28 -0
- package/dist/absence.js +266 -0
- package/dist/absence.js.map +1 -0
- package/dist/behavior.d.ts +26 -19
- package/dist/behavior.js +193 -203
- package/dist/behavior.js.map +1 -1
- package/dist/beside.d.ts +5 -0
- package/dist/beside.js +10 -0
- package/dist/beside.js.map +1 -1
- package/dist/border.d.ts +7 -8
- package/dist/border.js +88 -36
- package/dist/border.js.map +1 -1
- package/dist/bounds.d.ts +1 -0
- package/dist/bounds.js +27 -0
- package/dist/bounds.js.map +1 -0
- package/dist/changes.d.ts +23 -0
- package/dist/changes.js +355 -0
- package/dist/changes.js.map +1 -0
- package/dist/checks.d.ts +1 -0
- package/dist/checks.js +295 -0
- package/dist/checks.js.map +1 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +351 -14
- package/dist/cli.js.map +1 -1
- package/dist/codec.js +16 -7
- package/dist/codec.js.map +1 -1
- package/dist/codegen.js +6 -8
- package/dist/codegen.js.map +1 -1
- package/dist/composition.d.ts +3 -3
- package/dist/composition.js +9 -8
- package/dist/composition.js.map +1 -1
- package/dist/conformance.d.ts +24 -0
- package/dist/conformance.js +44 -0
- package/dist/conformance.js.map +1 -0
- package/dist/data.d.ts +3 -2
- package/dist/data.js +7 -3
- package/dist/data.js.map +1 -1
- package/dist/decimal-js.d.ts +2 -0
- package/dist/decimal-js.js +5 -0
- package/dist/decimal-js.js.map +1 -0
- package/dist/decimal.d.ts +2 -0
- package/dist/decimal.js +3 -0
- package/dist/decimal.js.map +1 -0
- package/dist/dependency.d.ts +9 -6
- package/dist/dependency.js +23 -17
- package/dist/dependency.js.map +1 -1
- package/dist/domain.d.ts +6 -2
- package/dist/domain.js +65 -25
- package/dist/domain.js.map +1 -1
- package/dist/ensures.js +3 -3
- package/dist/ensures.js.map +1 -1
- package/dist/equal.d.ts +1 -0
- package/dist/equal.js +135 -0
- package/dist/equal.js.map +1 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/exact.d.ts +13 -1
- package/dist/exact.js +52 -3
- package/dist/exact.js.map +1 -1
- package/dist/feasibility.d.ts +5 -3
- package/dist/feasibility.js +309 -53
- package/dist/feasibility.js.map +1 -1
- package/dist/guard-borders.d.ts +14 -5
- package/dist/guard-borders.js +229 -190
- package/dist/guard-borders.js.map +1 -1
- package/dist/guard-scope.d.ts +65 -0
- package/dist/guard-scope.js +384 -0
- package/dist/guard-scope.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.js +2 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +30 -8
- package/dist/index.js +18 -5
- package/dist/index.js.map +1 -1
- package/dist/interleave.d.ts +31 -0
- package/dist/interleave.js +202 -0
- package/dist/interleave.js.map +1 -0
- package/dist/interval.d.ts +8 -0
- package/dist/interval.js +213 -23
- package/dist/interval.js.map +1 -1
- package/dist/isolated.d.ts +7 -1
- package/dist/isolated.js +53 -5
- package/dist/isolated.js.map +1 -1
- package/dist/kinds/decimal.d.ts +1 -0
- package/dist/kinds/decimal.js +16 -0
- package/dist/kinds/decimal.js.map +1 -0
- package/dist/kinds/rational.d.ts +1 -0
- package/dist/kinds/rational.js +13 -0
- package/dist/kinds/rational.js.map +1 -0
- package/dist/kinds/temporal.d.ts +1 -0
- package/dist/kinds/temporal.js +15 -0
- package/dist/kinds/temporal.js.map +1 -0
- package/dist/lean/behavior.d.ts +29 -0
- package/dist/lean/behavior.js +76 -0
- package/dist/lean/behavior.js.map +1 -0
- package/dist/lean/given.d.ts +10 -0
- package/dist/lean/given.js +15 -0
- package/dist/lean/given.js.map +1 -0
- package/dist/lean/index.d.ts +11 -0
- package/dist/lean/index.js +8 -0
- package/dist/lean/index.js.map +1 -0
- package/dist/lean/model.d.ts +20 -0
- package/dist/lean/model.js +133 -0
- package/dist/lean/model.js.map +1 -0
- package/dist/lean/protocol.d.ts +14 -0
- package/dist/lean/protocol.js +34 -0
- package/dist/lean/protocol.js.map +1 -0
- package/dist/lean/rule.d.ts +54 -0
- package/dist/lean/rule.js +149 -0
- package/dist/lean/rule.js.map +1 -0
- package/dist/lean/schema.d.ts +46 -0
- package/dist/lean/schema.js +155 -0
- package/dist/lean/schema.js.map +1 -0
- package/dist/machine-check.d.ts +73 -0
- package/dist/machine-check.js +215 -0
- package/dist/machine-check.js.map +1 -0
- package/dist/machine.d.ts +122 -0
- package/dist/machine.js +271 -0
- package/dist/machine.js.map +1 -0
- package/dist/model.d.ts +110 -11
- package/dist/model.js +724 -80
- package/dist/model.js.map +1 -1
- package/dist/off-grid.d.ts +4 -0
- package/dist/off-grid.js +92 -0
- package/dist/off-grid.js.map +1 -0
- package/dist/outcomes.d.ts +15 -0
- package/dist/outcomes.js +120 -0
- package/dist/outcomes.js.map +1 -0
- package/dist/pairs.js +14 -5
- package/dist/pairs.js.map +1 -1
- package/dist/partition.d.ts +1 -0
- package/dist/partition.js +101 -19
- package/dist/partition.js.map +1 -1
- package/dist/placeholders.d.ts +3 -0
- package/dist/placeholders.js +44 -0
- package/dist/placeholders.js.map +1 -0
- package/dist/proof.d.ts +6 -0
- package/dist/proof.js +282 -56
- package/dist/proof.js.map +1 -1
- package/dist/protocol.d.ts +22 -0
- package/dist/protocol.js +12 -0
- package/dist/protocol.js.map +1 -0
- package/dist/relations.d.ts +30 -0
- package/dist/relations.js +235 -0
- package/dist/relations.js.map +1 -0
- package/dist/replay.d.ts +45 -0
- package/dist/replay.js +157 -0
- package/dist/replay.js.map +1 -0
- package/dist/rewrites.d.ts +41 -0
- package/dist/rewrites.js +180 -0
- package/dist/rewrites.js.map +1 -0
- package/dist/rule.d.ts +74 -11
- package/dist/rule.js +429 -22
- package/dist/rule.js.map +1 -1
- package/dist/runtime.d.ts +12 -0
- package/dist/runtime.js +8 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schema.d.ts +44 -28
- package/dist/schema.js +50 -113
- package/dist/schema.js.map +1 -1
- package/dist/specification.d.ts +23 -3
- package/dist/specification.js +1001 -96
- package/dist/specification.js.map +1 -1
- package/dist/stops.d.ts +20 -0
- package/dist/stops.js +221 -0
- package/dist/stops.js.map +1 -0
- package/dist/temporal-global.d.ts +4 -0
- package/dist/temporal-global.js +11 -0
- package/dist/temporal-global.js.map +1 -0
- package/dist/temporal.d.ts +22 -0
- package/dist/temporal.js +113 -0
- package/dist/temporal.js.map +1 -0
- package/dist/tla-validate.d.ts +31 -0
- package/dist/tla-validate.js +151 -0
- package/dist/tla-validate.js.map +1 -0
- package/dist/tla.d.ts +61 -0
- package/dist/tla.js +722 -0
- package/dist/tla.js.map +1 -0
- package/dist/verification.d.ts +9 -0
- package/dist/verification.js +7 -0
- package/dist/verification.js.map +1 -0
- package/dist/ways.d.ts +11 -5
- package/dist/ways.js +51 -45
- package/dist/ways.js.map +1 -1
- package/dist/world.d.ts +98 -0
- package/dist/world.js +463 -0
- package/dist/world.js.map +1 -0
- package/package.json +49 -10
- package/schema/report.schema.json +169 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { domainOf } from "./domain.js";
|
|
2
|
+
import { EvaluationLimit } from "./exact.js";
|
|
3
|
+
import { deepEqual } from "./equal.js";
|
|
4
|
+
import { MachineFailure, MachineLimitError, isMachine, positiveLimit, runMachineOutcomes } from "./machine.js";
|
|
5
|
+
import { rootTerm } from "./rule.js";
|
|
6
|
+
import { isVariantsSchema, tagOf } from "./schema.js";
|
|
7
|
+
import { checks, pairable, refusedBy } from "./world.js";
|
|
8
|
+
import { SpecificationError } from "./behavior.js";
|
|
9
|
+
export function specifyMachine(machine, properties = {}) {
|
|
10
|
+
const names = [...properties.invariants ?? [], ...properties.transitions ?? []].map(item => item.name);
|
|
11
|
+
const repeated = names.find((item, index) => names.indexOf(item) !== index);
|
|
12
|
+
if (repeated !== undefined)
|
|
13
|
+
throw new SpecificationError(`Machine ${machine.name} names property ${repeated} more than once`);
|
|
14
|
+
for (const transition of properties.transitions ?? []) {
|
|
15
|
+
if (transition.each)
|
|
16
|
+
pairable(machine.name, machine.state, transition);
|
|
17
|
+
}
|
|
18
|
+
const commandSchema = machine.command;
|
|
19
|
+
const tags = isVariantsSchema(commandSchema) ? commandSchema.variantTags : [];
|
|
20
|
+
const requires = {};
|
|
21
|
+
for (const [tag, precondition] of Object.entries(properties.requires ?? {})) {
|
|
22
|
+
if (!tags.includes(tag))
|
|
23
|
+
throw new SpecificationError(`Machine ${machine.name} declares no command ${tag}`);
|
|
24
|
+
requires[tag] = precondition(rootTerm("state"), rootTerm("command"));
|
|
25
|
+
}
|
|
26
|
+
const stateSchema = machine.state;
|
|
27
|
+
const fields = isVariantsSchema(stateSchema) ? [stateSchema.discriminant] : Object.keys(stateSchema.shape ?? {});
|
|
28
|
+
for (const column of properties.columns ?? []) {
|
|
29
|
+
if (!fields.includes(column))
|
|
30
|
+
throw new SpecificationError(`Machine ${machine.name} has no state field ${column} to use as a column`);
|
|
31
|
+
}
|
|
32
|
+
if (properties.interleavings !== undefined) {
|
|
33
|
+
if (properties.interleavings.copies.length === 0)
|
|
34
|
+
throw new SpecificationError(`Machine ${machine.name} has no copy to run`);
|
|
35
|
+
if (properties.interleavings.starts?.length === 0)
|
|
36
|
+
throw new SpecificationError(`Machine ${machine.name} has no state to start from`);
|
|
37
|
+
properties.interleavings.starts?.forEach((start, index) => {
|
|
38
|
+
const parsed = machine.state.parse(start);
|
|
39
|
+
if (!parsed.success)
|
|
40
|
+
throw new SpecificationError(`Start ${index}: ${parsed.issues.map(issue => `${issue.path}: ${issue.message}`).join("; ")}`);
|
|
41
|
+
});
|
|
42
|
+
properties.interleavings.copies.forEach((command, index) => {
|
|
43
|
+
const parsed = machine.command.parse(command);
|
|
44
|
+
if (!parsed.success)
|
|
45
|
+
throw new SpecificationError(`Copy ${index}: ${parsed.issues.map(issue => `${issue.path}: ${issue.message}`).join("; ")}`);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return { kind: "machine-specification", name: properties.name ?? machine.name, machine,
|
|
49
|
+
invariants: properties.invariants ?? [], transitions: properties.transitions ?? [], requires, columns: properties.columns ?? [], crashes: properties.crashes ?? false,
|
|
50
|
+
...(properties.interleavings === undefined ? {} : { interleavings: properties.interleavings }) };
|
|
51
|
+
}
|
|
52
|
+
export function isMachineSpecification(value) {
|
|
53
|
+
if (typeof value !== "object" || value === null)
|
|
54
|
+
return false;
|
|
55
|
+
const candidate = value;
|
|
56
|
+
return candidate.kind === "machine-specification" && isMachine(candidate.machine) &&
|
|
57
|
+
Array.isArray(candidate.invariants) && Array.isArray(candidate.transitions);
|
|
58
|
+
}
|
|
59
|
+
function violation(specification, steps) {
|
|
60
|
+
for (const step of steps) {
|
|
61
|
+
const failed = step.performed?.find(item => item.outcome === "failed");
|
|
62
|
+
const stopped = failed !== undefined || step.died !== undefined;
|
|
63
|
+
const tag = tagOf(specification.machine.command, step.command);
|
|
64
|
+
const cause = failed !== undefined ? `${failed.name} failed` : step.died === undefined ? undefined
|
|
65
|
+
: `${tag ?? (typeof step.command === "string" ? step.command : "the command")} died ${step.died.after === 0 ? "before its first effect" : `after effect ${step.died.after} (${step.performed[step.died.after - 1].name})`}`;
|
|
66
|
+
const after = (reason) => cause === undefined ? reason : `${reason} after ${cause}`;
|
|
67
|
+
const broken = specification.invariants.find(invariant => !checks(invariant.holds, step.after));
|
|
68
|
+
if (broken)
|
|
69
|
+
return after(`Invariant ${broken.name} failed`);
|
|
70
|
+
for (const transition of specification.transitions) {
|
|
71
|
+
const reason = refusedBy(transition, specification.name, step.before, step.after);
|
|
72
|
+
if (reason !== undefined)
|
|
73
|
+
return after(reason);
|
|
74
|
+
}
|
|
75
|
+
const precondition = tag === undefined ? undefined : specification.requires[tag];
|
|
76
|
+
if (precondition !== undefined) {
|
|
77
|
+
const held = checks(precondition, { state: step.before, command: step.command });
|
|
78
|
+
const changed = !deepEqual(step.before, step.after);
|
|
79
|
+
if (!held && changed)
|
|
80
|
+
return after(`${tag} changed the state although its precondition was false`);
|
|
81
|
+
// A command that stopped at a failed effect may have written nothing yet.
|
|
82
|
+
if (held && !changed && !stopped)
|
|
83
|
+
return `${tag} left the state unchanged although its precondition held`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
// Breadth-first search gives the shortest command sequence to a failure.
|
|
89
|
+
export function checkMachine(specification, options = {}) {
|
|
90
|
+
const maxStates = positiveLimit(options.maxStates, 10000, "maxStates");
|
|
91
|
+
const domainLimit = positiveLimit(options.domainLimit, 10000, "domainLimit");
|
|
92
|
+
const maxCommands = positiveLimit(options.maxCommands, 1000, "maxCommands");
|
|
93
|
+
const maxOutcomes = positiveLimit(options.maxOutcomes, 1000, "maxOutcomes");
|
|
94
|
+
const commands = domainOf(specification.machine.command, domainLimit);
|
|
95
|
+
const commandList = commands.values;
|
|
96
|
+
const seen = new Map();
|
|
97
|
+
let stateCount = 0;
|
|
98
|
+
const queue = [];
|
|
99
|
+
const keyOf = (state) => JSON.stringify(state, (_key, value) => typeof value === "bigint" ? `${value}n` : value) ?? "undefined";
|
|
100
|
+
const contains = (state) => (seen.get(keyOf(state)) ?? []).some(other => deepEqual(other, state));
|
|
101
|
+
const remember = (state) => {
|
|
102
|
+
const key = keyOf(state);
|
|
103
|
+
seen.set(key, [...seen.get(key) ?? [], state]);
|
|
104
|
+
stateCount++;
|
|
105
|
+
};
|
|
106
|
+
const trace = (index, command, steps) => {
|
|
107
|
+
const commands = [command];
|
|
108
|
+
const allSteps = [[...steps]];
|
|
109
|
+
for (let current = index; current !== undefined; current = queue[current].parent) {
|
|
110
|
+
const item = queue[current];
|
|
111
|
+
if (item.parent !== undefined) {
|
|
112
|
+
commands.unshift(item.command);
|
|
113
|
+
allSteps.unshift([...item.steps]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { commands, steps: allSteps.flat() };
|
|
117
|
+
};
|
|
118
|
+
let incomplete;
|
|
119
|
+
for (const start of specification.machine.initial) {
|
|
120
|
+
let broken;
|
|
121
|
+
try {
|
|
122
|
+
broken = specification.invariants.find(invariant => !checks(invariant.holds, start));
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (!(error instanceof EvaluationLimit))
|
|
126
|
+
throw error;
|
|
127
|
+
incomplete ??= error.message;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (broken)
|
|
131
|
+
return { status: "broken", states: 0, transitions: 0,
|
|
132
|
+
counterexample: { start, commands: [], steps: [], reason: `Initial state violates ${broken.name}` } };
|
|
133
|
+
if (contains(start))
|
|
134
|
+
continue;
|
|
135
|
+
if (stateCount === maxStates) {
|
|
136
|
+
incomplete ??= `Reached maxStates (${maxStates}) before exploring every initial state`;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
remember(start);
|
|
140
|
+
queue.push({ state: start, start, steps: [] });
|
|
141
|
+
}
|
|
142
|
+
const tabulated = new Map();
|
|
143
|
+
const tabulate = (state, commandIndex, changed, precondition) => {
|
|
144
|
+
const projected = Object.fromEntries(specification.columns.map(column => [column, state[column]]));
|
|
145
|
+
const key = keyOf(projected);
|
|
146
|
+
const row = tabulated.get(key) ?? { state: projected, cells: commandList.map(() => ({ changes: new Set(), precondition: new Set() })) };
|
|
147
|
+
tabulated.set(key, row);
|
|
148
|
+
row.cells[commandIndex].changes.add(changed);
|
|
149
|
+
if (precondition !== undefined)
|
|
150
|
+
row.cells[commandIndex].precondition.add(precondition);
|
|
151
|
+
};
|
|
152
|
+
const occurrence = (seen) => seen.size === 2 ? "sometimes" : seen.has(true) ? "always" : seen.has(false) ? "never" : "unknown";
|
|
153
|
+
const matrix = () => specification.columns.length === 0 ? {} : { matrix: {
|
|
154
|
+
columns: specification.columns,
|
|
155
|
+
rows: [...tabulated.values()].map(row => ({ state: row.state, cells: row.cells.map((cell, index) => ({
|
|
156
|
+
command: commandList[index], changes: occurrence(cell.changes),
|
|
157
|
+
...(cell.precondition.size === 0 ? {} : { precondition: occurrence(cell.precondition) }),
|
|
158
|
+
})) })),
|
|
159
|
+
} };
|
|
160
|
+
let transitions = 0;
|
|
161
|
+
for (let index = 0; index < queue.length; index++) {
|
|
162
|
+
const path = queue[index];
|
|
163
|
+
for (const [commandIndex, command] of commandList.entries()) {
|
|
164
|
+
for (const outcome of runMachineOutcomes(specification.machine, path.state, command, { maxCommands, maxOutcomes, crashes: specification.crashes })) {
|
|
165
|
+
transitions++;
|
|
166
|
+
const result = outcome.result;
|
|
167
|
+
const failure = outcome.error;
|
|
168
|
+
const steps = result?.steps ??
|
|
169
|
+
(failure instanceof MachineFailure || failure instanceof MachineLimitError ? failure.steps : []);
|
|
170
|
+
let broken;
|
|
171
|
+
try {
|
|
172
|
+
broken = violation(specification, steps);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (!(error instanceof EvaluationLimit))
|
|
176
|
+
throw error;
|
|
177
|
+
incomplete ??= error.message;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (broken || (failure && !(failure instanceof MachineLimitError)))
|
|
181
|
+
return {
|
|
182
|
+
status: "broken", states: stateCount, transitions,
|
|
183
|
+
counterexample: { start: path.start, ...trace(index, command, steps),
|
|
184
|
+
reason: broken ?? (failure instanceof Error ? failure.message : String(failure)) },
|
|
185
|
+
};
|
|
186
|
+
if (failure instanceof MachineLimitError) {
|
|
187
|
+
incomplete ??= failure.message;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (result === undefined)
|
|
191
|
+
continue;
|
|
192
|
+
if (specification.columns.length > 0) {
|
|
193
|
+
const tag = tagOf(specification.machine.command, command);
|
|
194
|
+
const precondition = tag === undefined ? undefined : specification.requires[tag];
|
|
195
|
+
tabulate(path.state, commandIndex, !deepEqual(path.state, result.state), precondition === undefined ? undefined : checks(precondition, { state: path.state, command }));
|
|
196
|
+
}
|
|
197
|
+
const next = result.state;
|
|
198
|
+
if (contains(next))
|
|
199
|
+
continue;
|
|
200
|
+
if (stateCount === maxStates) {
|
|
201
|
+
incomplete ??= `Reached maxStates (${maxStates}) before exploring every reachable state`;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
remember(next);
|
|
205
|
+
queue.push({ state: next, start: path.start, parent: index, command, steps });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (!commands.exhaustive)
|
|
210
|
+
incomplete ??= `Command schema could not be exhaustively enumerated within domainLimit (${domainLimit})`;
|
|
211
|
+
return incomplete === undefined
|
|
212
|
+
? { status: "held", states: stateCount, transitions, ...matrix() }
|
|
213
|
+
: { status: "undetermined", states: stateCount, transitions, reason: incomplete, ...matrix() };
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=machine-check.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"machine-check.js","sourceRoot":"","sources":["../src/machine-check.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,SAAS,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE/G,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAErC,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAyBnD,MAAM,UAAU,cAAc,CAAiB,OAAgC,EAAE,aAW7E,EAAE;IACJ,MAAM,KAAK,GAAG,CAAC,GAAG,UAAU,CAAC,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gBAC1B,IAAI,QAAQ,CAAC,IAAI,CAAC;oBAAE,SAAS;gBAC7B,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;oBAC7B,UAAU,KAAK,sBAAsB,SAAS,0CAA0C,CAAC;oBACzF,SAAS;gBACX,CAAC;gBACD,QAAQ,CAAC,IAAI,CAAC,CAAC;gBACf,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;YAChF,CAAC;QACH,CAAC;IACH,CAAC;IACD,IAAI,CAAC,QAAQ,CAAC,UAAU;QAAE,UAAU,KAAK,2EAA2E,WAAW,GAAG,CAAC;IACnI,OAAO,UAAU,KAAK,SAAS;QAC7B,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,MAAM,EAAE,EAAE;QAClE,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,EAAE,CAAC;AACnG,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { SpecificationError } from "./behavior.js";
|
|
2
|
+
import type { Template } from "./model.js";
|
|
3
|
+
import type { TermOf } from "./rule.js";
|
|
4
|
+
import type { Schema } from "./schema.js";
|
|
5
|
+
export interface Effect<State> {
|
|
6
|
+
readonly kind: "effect";
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly state: State;
|
|
9
|
+
readonly failure?: State;
|
|
10
|
+
readonly ifUnchanged?: boolean;
|
|
11
|
+
readonly writes?: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
export declare const isEffectRecord: (value: unknown) => boolean;
|
|
14
|
+
export declare function effect<State>(name: string, state: Template<NoInfer<State>>, options?: {
|
|
15
|
+
readonly failure?: Template<NoInfer<State>>;
|
|
16
|
+
readonly ifUnchanged?: boolean;
|
|
17
|
+
readonly writes?: readonly string[];
|
|
18
|
+
}): Template<Effect<State>>;
|
|
19
|
+
export type MachineCommand<State, Command> = Template<{
|
|
20
|
+
readonly state: State;
|
|
21
|
+
readonly commands?: readonly Command[];
|
|
22
|
+
} | {
|
|
23
|
+
readonly effects: readonly Effect<State>[];
|
|
24
|
+
readonly commands?: readonly Command[];
|
|
25
|
+
}>;
|
|
26
|
+
export interface Machine<State, Command> {
|
|
27
|
+
readonly kind: "machine";
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly state: Schema<State>;
|
|
30
|
+
readonly command: Schema<Command>;
|
|
31
|
+
readonly initial: readonly State[];
|
|
32
|
+
readonly update: MachineCommand<State, Command>;
|
|
33
|
+
}
|
|
34
|
+
export declare function machine<State, Command>(name: string, definition: {
|
|
35
|
+
readonly state: Schema<State>;
|
|
36
|
+
readonly command: Schema<Command>;
|
|
37
|
+
readonly initial: readonly State[];
|
|
38
|
+
readonly update: (state: TermOf<State>, command: TermOf<Command>) => MachineCommand<State, Command>;
|
|
39
|
+
}): Machine<State, Command>;
|
|
40
|
+
export declare function isMachine(value: unknown): value is Machine<unknown, unknown>;
|
|
41
|
+
export interface PerformedEffect {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly outcome: "succeeded" | "failed";
|
|
44
|
+
}
|
|
45
|
+
export interface MachineStep<State, Command> {
|
|
46
|
+
readonly command: Command;
|
|
47
|
+
readonly before: State;
|
|
48
|
+
readonly after: State;
|
|
49
|
+
readonly performed?: readonly PerformedEffect[];
|
|
50
|
+
readonly died?: {
|
|
51
|
+
readonly after: number;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface MachineExecution<State, Command> {
|
|
55
|
+
readonly state: State;
|
|
56
|
+
readonly steps: readonly MachineStep<State, Command>[];
|
|
57
|
+
}
|
|
58
|
+
export declare class MachineFailure<State, Command> extends SpecificationError {
|
|
59
|
+
readonly steps: readonly MachineStep<State, Command>[];
|
|
60
|
+
constructor(message: string, steps: readonly MachineStep<State, Command>[]);
|
|
61
|
+
}
|
|
62
|
+
export declare class MachineLimitError<State = unknown, Command = unknown> extends SpecificationError {
|
|
63
|
+
readonly steps: readonly MachineStep<State, Command>[];
|
|
64
|
+
constructor(message: string, steps?: readonly MachineStep<State, Command>[]);
|
|
65
|
+
}
|
|
66
|
+
export declare function runMachine<State, Command>(definition: Machine<State, Command>, state: NoInfer<State>, command: NoInfer<Command>, options?: {
|
|
67
|
+
readonly maxCommands?: number;
|
|
68
|
+
readonly fails?: (effect: {
|
|
69
|
+
readonly name: string;
|
|
70
|
+
readonly index: number;
|
|
71
|
+
readonly command: Command;
|
|
72
|
+
}) => boolean;
|
|
73
|
+
readonly dies?: (point: {
|
|
74
|
+
readonly completed: number;
|
|
75
|
+
readonly command: Command;
|
|
76
|
+
}) => boolean;
|
|
77
|
+
}): MachineExecution<State, Command>;
|
|
78
|
+
export interface EffectToPerform<State, Command> {
|
|
79
|
+
readonly name: string;
|
|
80
|
+
readonly index: number;
|
|
81
|
+
readonly command: Command;
|
|
82
|
+
readonly before: State;
|
|
83
|
+
readonly after: State;
|
|
84
|
+
}
|
|
85
|
+
export interface EffectFailure<Command> {
|
|
86
|
+
readonly name: string;
|
|
87
|
+
readonly index: number;
|
|
88
|
+
readonly command: Command;
|
|
89
|
+
readonly error: unknown;
|
|
90
|
+
}
|
|
91
|
+
export interface DrivenExecution<State, Command> extends MachineExecution<State, Command> {
|
|
92
|
+
readonly failures: readonly EffectFailure<Command>[];
|
|
93
|
+
}
|
|
94
|
+
export declare function driveMachine<State, Command>(definition: Machine<State, Command>, state: NoInfer<State>, command: NoInfer<Command>, options: {
|
|
95
|
+
readonly perform: (effect: EffectToPerform<State, Command>) => Promise<void | {
|
|
96
|
+
readonly commands?: readonly Command[];
|
|
97
|
+
}> | void | {
|
|
98
|
+
readonly commands?: readonly Command[];
|
|
99
|
+
};
|
|
100
|
+
readonly maxCommands?: number;
|
|
101
|
+
}): Promise<DrivenExecution<State, Command>>;
|
|
102
|
+
export interface EvaluatedCommand<State, Command> {
|
|
103
|
+
readonly effects?: readonly {
|
|
104
|
+
readonly name: string;
|
|
105
|
+
readonly state: State;
|
|
106
|
+
readonly ifUnchanged: boolean;
|
|
107
|
+
readonly writes?: readonly string[];
|
|
108
|
+
}[];
|
|
109
|
+
readonly state?: State;
|
|
110
|
+
readonly commands: readonly Command[];
|
|
111
|
+
}
|
|
112
|
+
export declare function evaluateCommand<State, Command>(definition: Machine<State, Command>, state: State, command: Command): EvaluatedCommand<State, Command>;
|
|
113
|
+
export interface MachineOutcome<State, Command> {
|
|
114
|
+
readonly result?: MachineExecution<State, Command>;
|
|
115
|
+
readonly error?: unknown;
|
|
116
|
+
}
|
|
117
|
+
export declare function runMachineOutcomes<State, Command>(definition: Machine<State, Command>, state: NoInfer<State>, command: NoInfer<Command>, options?: {
|
|
118
|
+
readonly maxCommands?: number;
|
|
119
|
+
readonly maxOutcomes?: number;
|
|
120
|
+
readonly crashes?: boolean;
|
|
121
|
+
}): MachineOutcome<State, Command>[];
|
|
122
|
+
export declare function positiveLimit(value: number | undefined, fallback: number, name: string): number;
|
package/dist/machine.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// A single executable expression describes both production transitions and
|
|
2
|
+
// the graph checked by exhaustive exploration. No second model can drift from
|
|
3
|
+
// the code that runs. Completeness requires a finite command domain.
|
|
4
|
+
import { SpecificationError } from "./behavior.js";
|
|
5
|
+
import { EvaluationLimit } from "./exact.js";
|
|
6
|
+
import { childrenOf, interpret, nodeOf } from "./model.js";
|
|
7
|
+
import { copyOf, rootTerm } from "./rule.js";
|
|
8
|
+
// The records `effect` makes, told apart from a state or command that happens to hold `kind: "effect"`: by being one of these, and no field a user's value may carry. Kept on the global object since a specification is loaded in a module graph of its own.
|
|
9
|
+
const RECORDS = (globalThis[Symbol.for("chisel.effect.records")] ??= new WeakSet());
|
|
10
|
+
export const isEffectRecord = (value) => typeof value === "object" && value !== null && RECORDS.has(value);
|
|
11
|
+
export function effect(name, state, options = {}) {
|
|
12
|
+
const record = { kind: "effect", name, state,
|
|
13
|
+
...(Object.hasOwn(options, "failure") ? { failure: options.failure } : {}),
|
|
14
|
+
...(options.ifUnchanged === undefined ? {} : { ifUnchanged: options.ifUnchanged }),
|
|
15
|
+
...(options.writes === undefined ? {} : { writes: options.writes }) };
|
|
16
|
+
RECORDS.add(record);
|
|
17
|
+
return record;
|
|
18
|
+
}
|
|
19
|
+
export function machine(name, definition) {
|
|
20
|
+
if (Object.hasOwn(definition, "invariants") || Object.hasOwn(definition, "transitions")) {
|
|
21
|
+
throw new SpecificationError(`Machine ${name} verification properties belong in a separate specifyMachine declaration`);
|
|
22
|
+
}
|
|
23
|
+
if (definition.initial.length === 0)
|
|
24
|
+
throw new SpecificationError(`Machine ${name} starts from no state`);
|
|
25
|
+
const initial = definition.initial.map((value, index) => parse(definition.state, value, `Initial state ${index + 1}`));
|
|
26
|
+
const update = definition.update(rootTerm("state"), rootTerm("command"));
|
|
27
|
+
if (!update || (!nodeOf(update) && !Object.hasOwn(update, "state") && !Object.hasOwn(update, "effects")))
|
|
28
|
+
throw new SpecificationError(`Machine ${name} must return a state`);
|
|
29
|
+
if (hasCall(update))
|
|
30
|
+
throw new SpecificationError(`Machine ${name} cannot call external dependencies in a transition`);
|
|
31
|
+
return { kind: "machine", name, state: definition.state, command: definition.command, initial, update };
|
|
32
|
+
}
|
|
33
|
+
export function isMachine(value) {
|
|
34
|
+
if (typeof value !== "object" || value === null)
|
|
35
|
+
return false;
|
|
36
|
+
const candidate = value;
|
|
37
|
+
return candidate.kind === "machine" && typeof candidate.name === "string" &&
|
|
38
|
+
typeof candidate.state?.parse === "function" && typeof candidate.command?.parse === "function" &&
|
|
39
|
+
Array.isArray(candidate.initial) && candidate.update !== undefined;
|
|
40
|
+
}
|
|
41
|
+
function hasCall(value) {
|
|
42
|
+
if (nodeOf(value)?.kind === "call")
|
|
43
|
+
return true;
|
|
44
|
+
return childrenOf(value).some(hasCall);
|
|
45
|
+
}
|
|
46
|
+
function parse(schema, value, context) {
|
|
47
|
+
const result = schema.parse(value);
|
|
48
|
+
if (!result.success)
|
|
49
|
+
throw new SpecificationError(`${context}: ${result.issues.map(issue => `${issue.path}: ${issue.message}`).join("; ")}`);
|
|
50
|
+
return result.value;
|
|
51
|
+
}
|
|
52
|
+
export class MachineFailure extends SpecificationError {
|
|
53
|
+
steps;
|
|
54
|
+
constructor(message, steps) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.steps = steps;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class MachineLimitError extends SpecificationError {
|
|
60
|
+
steps;
|
|
61
|
+
constructor(message, steps = []) {
|
|
62
|
+
super(message);
|
|
63
|
+
this.steps = steps;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Evaluates the same expression used by the verification tools. Emissions are internal
|
|
67
|
+
// commands, not I/O. The caller decides when and where to persist the result.
|
|
68
|
+
// `fails` says whether an effect fails and `dies` whether the process dies at a point of a
|
|
69
|
+
// command with effects (before the first, and after each); by default neither happens, since
|
|
70
|
+
// an effect is something the caller performs and this function only follows the machine.
|
|
71
|
+
export function runMachine(definition, state, command, options = {}) {
|
|
72
|
+
const run = follow(definition, state, command, positiveLimit(options.maxCommands, 1000, "maxCommands"));
|
|
73
|
+
let next = run.next();
|
|
74
|
+
while (!next.done) {
|
|
75
|
+
const point = next.value;
|
|
76
|
+
let answer;
|
|
77
|
+
try {
|
|
78
|
+
answer = point.kind === "dies"
|
|
79
|
+
? options.dies?.({ completed: point.completed, command: point.command }) === true
|
|
80
|
+
: { failed: options.fails?.({ name: point.name, index: point.index, command: point.command }) === true };
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
// Thrown back into the run so it is read as any other failure of the command, with the steps already run.
|
|
84
|
+
next = run.throw(error);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
next = run.next(answer);
|
|
88
|
+
}
|
|
89
|
+
return next.value;
|
|
90
|
+
}
|
|
91
|
+
// Runs the machine in production: the same transition expression `runMachine` and the checks
|
|
92
|
+
// follow decides every next effect, and `perform` carries it out. An effect whose `perform`
|
|
93
|
+
// returns succeeded (and may hand back commands it observed, which run after the commands the
|
|
94
|
+
// transition emits, as every command does); one whose `perform` throws or rejects failed, which
|
|
95
|
+
// ends its command in the effect's failure state exactly as `runMachine` with `fails` does.
|
|
96
|
+
export async function driveMachine(definition, state, command, options) {
|
|
97
|
+
const run = follow(definition, state, command, positiveLimit(options.maxCommands, 1000, "maxCommands"));
|
|
98
|
+
const failures = [];
|
|
99
|
+
let next = run.next();
|
|
100
|
+
while (!next.done) {
|
|
101
|
+
const point = next.value;
|
|
102
|
+
if (point.kind === "dies") {
|
|
103
|
+
next = run.next(false);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
let answer;
|
|
107
|
+
try {
|
|
108
|
+
const performed = await options.perform({ name: point.name, index: point.index, command: copyOf(point.command), before: copyOf(point.before), after: copyOf(point.after) });
|
|
109
|
+
answer = { failed: false, ...(performed && performed.commands !== undefined ? { commands: performed.commands } : {}) };
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
failures.push({ name: point.name, index: point.index, command: point.command, error });
|
|
113
|
+
answer = { failed: true };
|
|
114
|
+
}
|
|
115
|
+
next = run.next(answer);
|
|
116
|
+
}
|
|
117
|
+
return { ...next.value, failures };
|
|
118
|
+
}
|
|
119
|
+
// The one loop every way of running a machine shares: it stops at each point where the process
|
|
120
|
+
// may die and at each effect, and the caller answers whether it died or the effect failed.
|
|
121
|
+
function* follow(definition, state, command, maxCommands) {
|
|
122
|
+
let current = parse(definition.state, state, "Current state");
|
|
123
|
+
const pending = [command];
|
|
124
|
+
const steps = [];
|
|
125
|
+
while (pending.length > 0) {
|
|
126
|
+
if (steps.length === maxCommands)
|
|
127
|
+
throw new MachineLimitError(`Machine ${definition.name} exceeded ${maxCommands} commands`, [...steps]);
|
|
128
|
+
try {
|
|
129
|
+
const input = parse(definition.command, pending.shift(), "Command");
|
|
130
|
+
const output = interpret(definition.update, { state: current, command: input });
|
|
131
|
+
if (output === null || typeof output !== "object" || (!Object.hasOwn(output, "state") && !Object.hasOwn(output, "effects"))) {
|
|
132
|
+
throw new SpecificationError("A machine transition must return a state");
|
|
133
|
+
}
|
|
134
|
+
let next;
|
|
135
|
+
let performed;
|
|
136
|
+
let stopped = false;
|
|
137
|
+
let died;
|
|
138
|
+
const observed = [];
|
|
139
|
+
const emittedBy = () => {
|
|
140
|
+
const emitted = output.commands === undefined ? [] : output.commands;
|
|
141
|
+
if (!Array.isArray(emitted))
|
|
142
|
+
throw new SpecificationError("Emitted commands must be an array");
|
|
143
|
+
return emitted.map((value) => parse(definition.command, value, "Emitted command"));
|
|
144
|
+
};
|
|
145
|
+
let emittedCommands = [];
|
|
146
|
+
if (Object.hasOwn(output, "effects")) {
|
|
147
|
+
// The whole transition is read against the schemas before the first effect is performed, so
|
|
148
|
+
// a refused one never leaves an effect done behind a command or a later effect that fails.
|
|
149
|
+
emittedCommands = emittedBy();
|
|
150
|
+
if (!Array.isArray(output.effects))
|
|
151
|
+
throw new SpecificationError("Effects must be an array");
|
|
152
|
+
next = current;
|
|
153
|
+
performed = [];
|
|
154
|
+
const declaredEffects = output.effects.map(item => {
|
|
155
|
+
const declared = item;
|
|
156
|
+
if (declared === null || typeof declared !== "object" || typeof declared.name !== "string")
|
|
157
|
+
throw new SpecificationError("An effect must name itself");
|
|
158
|
+
return { name: declared.name, after: parse(definition.state, declared.state, `Effect ${declared.name} state`),
|
|
159
|
+
...(Object.hasOwn(declared, "failure") ? { failure: parse(definition.state, declared.failure, `Effect ${declared.name} failure state`) } : {}) };
|
|
160
|
+
});
|
|
161
|
+
for (const [index, declared] of declaredEffects.entries()) {
|
|
162
|
+
if ((yield { kind: "dies", completed: index, command: input }) === true) {
|
|
163
|
+
died = { after: index };
|
|
164
|
+
stopped = true;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
const answer = (yield { kind: "effect", name: declared.name, index, command: input, before: next, after: declared.after });
|
|
168
|
+
if (answer.failed) {
|
|
169
|
+
performed.push({ name: declared.name, outcome: "failed" });
|
|
170
|
+
if (Object.hasOwn(declared, "failure"))
|
|
171
|
+
next = declared.failure;
|
|
172
|
+
stopped = true;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
next = declared.after;
|
|
176
|
+
performed.push({ name: declared.name, outcome: "succeeded" });
|
|
177
|
+
if (answer.commands !== undefined)
|
|
178
|
+
observed.push(...answer.commands);
|
|
179
|
+
}
|
|
180
|
+
if (!stopped && declaredEffects.length > 0 && (yield { kind: "dies", completed: declaredEffects.length, command: input }) === true) {
|
|
181
|
+
died = { after: declaredEffects.length };
|
|
182
|
+
stopped = true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else
|
|
186
|
+
next = parse(definition.state, output.state, `Command ${steps.length + 1} next state`);
|
|
187
|
+
steps.push({ command: input, before: current, after: next, ...(performed === undefined ? {} : { performed }), ...(died === undefined ? {} : { died }) });
|
|
188
|
+
if (died !== undefined)
|
|
189
|
+
pending.length = 0;
|
|
190
|
+
if (performed === undefined)
|
|
191
|
+
emittedCommands = emittedBy();
|
|
192
|
+
const observedCommands = (died === undefined ? observed : []).map((value) => parse(definition.command, value, "Emitted command"));
|
|
193
|
+
const validated = [...(stopped ? [] : emittedCommands), ...observedCommands];
|
|
194
|
+
current = next;
|
|
195
|
+
pending.push(...validated);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error instanceof EvaluationLimit)
|
|
199
|
+
throw new MachineLimitError(error.message, [...steps]);
|
|
200
|
+
throw new MachineFailure(error instanceof Error ? error.message : String(error), steps);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { state: current, steps };
|
|
204
|
+
}
|
|
205
|
+
// What a command does in a state, read once and checked against the schemas.
|
|
206
|
+
export function evaluateCommand(definition, state, command) {
|
|
207
|
+
const output = interpret(definition.update, { state, command });
|
|
208
|
+
if (output === null || typeof output !== "object" || (!Object.hasOwn(output, "state") && !Object.hasOwn(output, "effects"))) {
|
|
209
|
+
throw new SpecificationError("A machine transition must return a state");
|
|
210
|
+
}
|
|
211
|
+
const emitted = output.commands === undefined ? [] : output.commands;
|
|
212
|
+
if (!Array.isArray(emitted))
|
|
213
|
+
throw new SpecificationError("Emitted commands must be an array");
|
|
214
|
+
const commands = emitted.map((value) => parse(definition.command, value, "Emitted command"));
|
|
215
|
+
if (!Object.hasOwn(output, "effects"))
|
|
216
|
+
return { state: parse(definition.state, output.state, "Next state"), commands };
|
|
217
|
+
if (!Array.isArray(output.effects))
|
|
218
|
+
throw new SpecificationError("Effects must be an array");
|
|
219
|
+
const effects = output.effects.map(item => {
|
|
220
|
+
const declared = item;
|
|
221
|
+
if (declared === null || typeof declared !== "object" || typeof declared.name !== "string")
|
|
222
|
+
throw new SpecificationError("An effect must name itself");
|
|
223
|
+
if (declared.ifUnchanged !== undefined && typeof declared.ifUnchanged !== "boolean")
|
|
224
|
+
throw new SpecificationError(`Effect ${declared.name}: ifUnchanged must be a boolean`);
|
|
225
|
+
if (declared.writes !== undefined && (!Array.isArray(declared.writes) || declared.writes.some(item => typeof item !== "string"))) {
|
|
226
|
+
throw new SpecificationError(`Effect ${declared.name}: writes must be a list of field names`);
|
|
227
|
+
}
|
|
228
|
+
return { name: declared.name, state: parse(definition.state, declared.state, `Effect ${declared.name} state`), ifUnchanged: declared.ifUnchanged === true,
|
|
229
|
+
...(declared.writes === undefined ? {} : { writes: declared.writes }) };
|
|
230
|
+
});
|
|
231
|
+
return { effects, commands };
|
|
232
|
+
}
|
|
233
|
+
// Every way a command can end when each effect it runs may succeed or fail (and, with `crashes`,
|
|
234
|
+
// the process may die before the first effect and after each one), found by replaying the run
|
|
235
|
+
// with each choice taken the other way once.
|
|
236
|
+
export function runMachineOutcomes(definition, state, command, options = {}) {
|
|
237
|
+
const maxOutcomes = positiveLimit(options.maxOutcomes, 1000, "maxOutcomes");
|
|
238
|
+
const outcomes = [];
|
|
239
|
+
const tapes = [[]];
|
|
240
|
+
for (let next = 0; next < tapes.length; next++) {
|
|
241
|
+
const tape = tapes[next];
|
|
242
|
+
if (outcomes.length === maxOutcomes) {
|
|
243
|
+
outcomes.push({ error: new MachineLimitError(`Machine ${definition.name} exceeded ${maxOutcomes} outcomes for one command`) });
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
const decided = tape;
|
|
247
|
+
let evaluated = 0;
|
|
248
|
+
const choose = () => {
|
|
249
|
+
const choice = evaluated++;
|
|
250
|
+
if (choice < decided.length)
|
|
251
|
+
return decided[choice];
|
|
252
|
+
tapes.push([...decided, ...Array.from({ length: choice - decided.length }, () => false), true]);
|
|
253
|
+
return false;
|
|
254
|
+
};
|
|
255
|
+
const { crashes, ...limits } = options;
|
|
256
|
+
try {
|
|
257
|
+
outcomes.push({ result: runMachine(definition, state, command, { ...limits, fails: choose, ...(crashes ? { dies: choose } : {}) }) });
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
outcomes.push({ error });
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return outcomes;
|
|
264
|
+
}
|
|
265
|
+
export function positiveLimit(value, fallback, name) {
|
|
266
|
+
const result = value ?? fallback;
|
|
267
|
+
if (!Number.isSafeInteger(result) || result < 1)
|
|
268
|
+
throw new SpecificationError(`${name} must be a positive safe integer`);
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
//# sourceMappingURL=machine.js.map
|