tla-ts 0.0.8 → 0.0.9-dev.0dd11f695

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.
Files changed (119) hide show
  1. package/README.md +30 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +40 -40
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +98 -19
  8. package/dist/index.d.ts +98 -19
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +1 -1
  20. package/dist/transfer.cjs +128 -106
  21. package/dist/transfer.js +128 -106
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +605 -311
  30. package/src/cli/options.ts +12 -3
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlc-arguments.ts +646 -0
  35. package/src/cli/tlc-usage.ts +362 -0
  36. package/src/cli/tlc.ts +571 -185
  37. package/src/cli/typescript.ts +26 -0
  38. package/src/cli/xml-exporter.ts +4 -10
  39. package/src/codegen.ts +190 -51
  40. package/src/config.ts +101 -68
  41. package/src/coverage.ts +855 -0
  42. package/src/dependencies.ts +340 -24
  43. package/src/dfid.ts +636 -0
  44. package/src/environment.ts +289 -109
  45. package/src/errors.ts +142 -45
  46. package/src/evaluate.ts +734 -980
  47. package/src/exit-codes.ts +37 -1
  48. package/src/index.ts +3 -0
  49. package/src/instances.ts +85 -9
  50. package/src/java-overrides.ts +2 -12
  51. package/src/json-reader.ts +456 -0
  52. package/src/json.ts +262 -139
  53. package/src/levels.ts +2 -3
  54. package/src/lexer.ts +150 -70
  55. package/src/liveness-checker.ts +10 -18
  56. package/src/liveness.ts +122 -64
  57. package/src/parallel-worker.ts +14 -10
  58. package/src/parallel.ts +41 -17
  59. package/src/parser.ts +436 -90
  60. package/src/pcal/ast.ts +7 -28
  61. package/src/pcal/parse.ts +11 -39
  62. package/src/pcal/tokenize.ts +4 -20
  63. package/src/pcal/translate.ts +10 -42
  64. package/src/pcal/validate.ts +3 -21
  65. package/src/pretty-print.ts +1 -2
  66. package/src/prover/smt/solver/solver.ts +31 -27
  67. package/src/randomization.ts +84 -35
  68. package/src/resolve.ts +18 -60
  69. package/src/runtime.ts +11 -4
  70. package/src/sany/builtins.ts +3 -4
  71. package/src/sany/generator.ts +128 -176
  72. package/src/sany/levels.ts +6 -30
  73. package/src/sany/lexer.ts +7 -23
  74. package/src/sany/linter.ts +31 -29
  75. package/src/sany/modules.ts +1 -5
  76. package/src/sany/nodes.ts +4 -28
  77. package/src/sany/parse-error.ts +44 -100
  78. package/src/sany/report.ts +25 -40
  79. package/src/sany/selectors.ts +8 -22
  80. package/src/sany/source.ts +8 -15
  81. package/src/sany/xml.ts +11 -42
  82. package/src/search.ts +550 -150
  83. package/src/simulate.ts +694 -0
  84. package/src/spec-description.ts +167 -49
  85. package/src/standard-definitions.ts +2 -3
  86. package/src/standard-modules.ts +264 -68
  87. package/src/state-graph.ts +51 -18
  88. package/src/subexpressions.ts +31 -5
  89. package/src/successors.ts +269 -85
  90. package/src/symmetry.ts +59 -147
  91. package/src/tableau.ts +1 -2
  92. package/src/tlc/fingerprint.ts +226 -151
  93. package/src/tlc/jar-data.ts +113 -6
  94. package/src/tlc/messages.ts +122 -37
  95. package/src/tlc/order.ts +56 -82
  96. package/src/tlc/random.ts +16 -16
  97. package/src/tlc/replay.ts +564 -212
  98. package/src/tlc/summary.ts +2 -4
  99. package/src/tlc/tlc-operators.ts +81 -36
  100. package/src/tlc/tlcext-operators.ts +138 -117
  101. package/src/tlc/trace-spec.ts +273 -0
  102. package/src/tlc/value-stream.ts +143 -0
  103. package/src/tlc/version.ts +2 -4
  104. package/src/typescript/actions.ts +494 -0
  105. package/src/typescript/collections.ts +615 -0
  106. package/src/typescript/compile.ts +70 -0
  107. package/src/typescript/context.ts +106 -0
  108. package/src/typescript/emit.ts +122 -0
  109. package/src/typescript/expressions.ts +508 -0
  110. package/src/typescript/formula.ts +160 -0
  111. package/src/typescript/functions.ts +295 -0
  112. package/src/typescript/model.ts +164 -0
  113. package/src/typescript/names.ts +116 -0
  114. package/src/typescript/program.ts +51 -0
  115. package/src/typescript/properties.ts +148 -0
  116. package/src/typescript/scope.ts +117 -0
  117. package/src/typescript/steps.ts +175 -0
  118. package/src/typescript/types.ts +178 -0
  119. package/src/values.ts +910 -318
@@ -1,18 +1,75 @@
1
1
  import { getChildren, isLocallyResolved, type Definition, type Expression } from "./ast";
2
- import { isImpureBuiltin, type Environment } from "./evaluate";
2
+ import type { Environment } from "./evaluate";
3
+ import { ACTION_LEVEL, CONSTANT_LEVEL, TEMPORAL_LEVEL, VARIABLE_LEVEL } from "./levels";
3
4
 
4
- const ACTION_AND_TEMPORAL_KINDS = new Set([
5
+ interface DefinitionNames extends Pick<
6
+ Environment,
7
+ "definitions" | "substitutions" | "constants" | "variableIndex" | "builtins" | "variables"
8
+ > {}
9
+
10
+ // Each fact is reachability over the definitions a definition calls, so one pass over the call
11
+ // graph computes them all. A row's first word holds the flags below, and the rest a bitset of the
12
+ // variables the definition reads.
13
+ export interface DefinitionFacts {
14
+ ids: Map<Definition, number>;
15
+ rows: Uint32Array;
16
+ words: number;
17
+ }
18
+
19
+ // The definition reads a variable, a state-level builtin, or an action or temporal operator.
20
+ const NONCONSTANT = 1;
21
+ // Evaluating the definition again can print, assert, read the clock or choose at random.
22
+ const IMPURE = 2;
23
+ // Like IMPURE, but an Assert that held holds again, so repeating it looks the same.
24
+ const REPEATED_EFFECT = 4;
25
+ // The definition builds a set with UNION or \cup, which streams until TLC caches it.
26
+ const STREAMED = 8;
27
+ // The variables the definition reads don't decide its value: it has an effect, or it is an action.
28
+ const READS_IMPURE = 16;
29
+ // The liveness level of the definition, as thresholds so that reachability is a bitwise or.
30
+ const VARIABLE_LEVEL_FLAGS = 32;
31
+ const ACTION_LEVEL_FLAGS = VARIABLE_LEVEL_FLAGS | 64;
32
+ const TEMPORAL_LEVEL_FLAGS = ACTION_LEVEL_FLAGS | 128;
33
+
34
+ const ACTION_KINDS = new Set<Expression["kind"]>([
5
35
  "Prime",
6
36
  "Unchanged",
7
- "Enabled",
8
- "Always",
9
- "Eventually",
10
37
  "BoxAction",
11
38
  "AngleAction",
39
+ ]);
40
+
41
+ const TEMPORAL_KINDS = new Set<Expression["kind"]>([
42
+ "Always",
43
+ "Eventually",
12
44
  "WeakFairness",
13
45
  "StrongFairness",
14
46
  ]);
15
47
 
48
+ // TLC bounds the level of every action and temporal operator above constant, whatever its operands.
49
+ const NONCONSTANT_KINDS = new Set<Expression["kind"]>([
50
+ ...ACTION_KINDS,
51
+ ...TEMPORAL_KINDS,
52
+ "Enabled",
53
+ ]);
54
+
55
+ const TEMPORAL_OPERATORS = new Set(["~>", "-+->"]);
56
+
57
+ const NONCONSTANT_OPERATORS = new Set([...TEMPORAL_OPERATORS, "\\cdot"]);
58
+
59
+ const IMPURE_BUILTINS = new Set([
60
+ "Print",
61
+ "PrintT",
62
+ "Assert",
63
+ "JavaTime",
64
+ "RandomElement",
65
+ "TLCGet",
66
+ "TLCSet",
67
+ ]);
68
+
69
+ // UNION and \cup stream the sets they combine until TLC caches them, and TLC builds a new one at
70
+ // each evaluation.
71
+ const STREAMED_BUILTINS = new Set(["UNION", "\\cup"]);
72
+
16
73
  const getReferencedName = (expression: Expression) => {
17
74
  if (expression.kind === "Identifier") return expression.name;
18
75
  if (expression.kind === "Apply" || expression.kind === "OperatorReference")
@@ -20,30 +77,289 @@ const getReferencedName = (expression: Expression) => {
20
77
  return undefined;
21
78
  };
22
79
 
23
- export const getReadVariables = (
24
- expression: Expression,
25
- environment: Environment,
26
- ): number[] | undefined => {
27
- const variables = new Set<number>();
28
- const visited = new Set<Definition>();
29
- const visit = (node: Expression): boolean => {
30
- if (ACTION_AND_TEMPORAL_KINDS.has(node.kind)) return false;
80
+ const isImpureBuiltin = (name: string, environment: DefinitionNames) =>
81
+ IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
82
+
83
+ interface Edge {
84
+ callee: Definition;
85
+ // The configuration replaces the name by a constant value, which TLC's level and variable
86
+ // analyses take for a constant and its effect analysis evaluates.
87
+ isOverridden: boolean;
88
+ // Outside every action and temporal operator, where the level of the callee counts. An operator
89
+ // such as ENABLED fixes the level of what it holds.
90
+ isLevelCounted: boolean;
91
+ }
92
+
93
+ const collectLocalFacts = (
94
+ expressions: Expression[],
95
+ environment: DefinitionNames,
96
+ words: number,
97
+ ) => {
98
+ const row = new Uint32Array(words);
99
+ const edges: Edge[] = [];
100
+ // Undefined for a node whose level the nodes it holds decide. Nothing below an action or temporal
101
+ // operator, or ENABLED, counts towards the level, variables and callees included.
102
+ const getOwnLevelFlags = (node: Expression) => {
103
+ const kind = node.kind;
104
+ if (ACTION_KINDS.has(kind) || (kind === "Apply" && node.operator === "\\cdot")) {
105
+ return ACTION_LEVEL_FLAGS;
106
+ }
107
+ if (kind === "Enabled") return VARIABLE_LEVEL_FLAGS;
108
+ if (
109
+ TEMPORAL_KINDS.has(kind) ||
110
+ (kind === "UnboundedQuantifier" && node.isTemporal === true) ||
111
+ (kind === "Apply" && TEMPORAL_OPERATORS.has(node.operator))
112
+ ) {
113
+ return TEMPORAL_LEVEL_FLAGS;
114
+ }
115
+ return undefined;
116
+ };
117
+ const visit = (node: Expression, isLevelCounted: boolean) => {
118
+ const kind = node.kind;
119
+ if (
120
+ NONCONSTANT_KINDS.has(kind) ||
121
+ (kind === "UnboundedQuantifier" && node.isTemporal === true) ||
122
+ (kind === "Apply" && NONCONSTANT_OPERATORS.has(node.operator))
123
+ ) {
124
+ row[0] |= NONCONSTANT;
125
+ }
126
+ if (NONCONSTANT_KINDS.has(kind)) row[0] |= READS_IMPURE;
127
+ const ownLevelFlags = getOwnLevelFlags(node);
128
+ if (isLevelCounted && ownLevelFlags !== undefined) row[0] |= ownLevelFlags;
129
+ const isInnerLevelCounted = isLevelCounted && ownLevelFlags === undefined;
31
130
  const name = getReferencedName(node);
32
- if (name !== undefined && !isLocallyResolved(node) && !environment.constants.has(name)) {
131
+ if (name !== undefined && !isLocallyResolved(node)) {
33
132
  const target = environment.substitutions.get(name) ?? name;
34
133
  const definition = environment.definitions.get(target);
35
- if (!definition && isImpureBuiltin(target, environment)) return false;
134
+ const isOverridden = environment.constants.has(name);
36
135
  const variableIndex = environment.variableIndex.get(name);
37
- if (variableIndex !== undefined) variables.add(variableIndex);
38
- if (definition && !visited.has(definition)) {
39
- visited.add(definition);
40
- const isPure =
41
- visit(definition.body) &&
42
- (definition.kind === "Operator" || definition.bounds.every((bound) => visit(bound.set)));
43
- if (!isPure) return false;
136
+ // TLC's level bound takes an instance's definition named like a variable for the variable.
137
+ if (
138
+ variableIndex !== undefined ||
139
+ environment.builtins.get(name)?.isStateLevel ||
140
+ (definition && environment.variableIndex.has(definition.name))
141
+ ) {
142
+ row[0] |= NONCONSTANT;
143
+ }
144
+ if (variableIndex !== undefined) {
145
+ if (isInnerLevelCounted) row[0] |= VARIABLE_LEVEL_FLAGS;
146
+ if (!isOverridden) row[1 + (variableIndex >>> 5)] |= 1 << (variableIndex & 31);
147
+ }
148
+ if (definition) {
149
+ edges.push({ callee: definition, isOverridden, isLevelCounted: isInnerLevelCounted });
150
+ } else {
151
+ if (isImpureBuiltin(name, environment)) {
152
+ row[0] |= IMPURE | (name === "Assert" ? 0 : REPEATED_EFFECT);
153
+ }
154
+ if (STREAMED_BUILTINS.has(name)) row[0] |= STREAMED;
155
+ if (!isOverridden && isImpureBuiltin(target, environment)) row[0] |= READS_IMPURE;
156
+ const builtin = environment.builtins.get(target);
157
+ if (
158
+ isInnerLevelCounted &&
159
+ !isOverridden &&
160
+ variableIndex === undefined &&
161
+ builtin?.isStateLevel &&
162
+ !builtin.isLevelConstant
163
+ ) {
164
+ row[0] |= builtin.isActionLevel ? ACTION_LEVEL_FLAGS : VARIABLE_LEVEL_FLAGS;
165
+ }
44
166
  }
45
167
  }
46
- return getChildren(node).every(visit);
168
+ getChildren(node).forEach((child) => visit(child, isInnerLevelCounted));
47
169
  };
48
- return visit(expression) ? [...variables] : undefined;
170
+ expressions.forEach((expression) => visit(expression, true));
171
+ return { row, edges };
49
172
  };
173
+
174
+ const getDefinitionExpressions = (definition: Definition) =>
175
+ definition.kind === "Function"
176
+ ? [...definition.bounds.map((bound) => bound.set), definition.body]
177
+ : [definition.body];
178
+
179
+ // Tarjan's algorithm lists the strongly connected components with the callees first, so each
180
+ // component ors in components that are already final.
181
+ const closeRows = (local: Uint32Array, words: number, edges: Int32Array[], mask: number) => {
182
+ const count = edges.length;
183
+ const closed = local.slice();
184
+ const index = new Int32Array(count).fill(-1);
185
+ const lowLink = new Int32Array(count);
186
+ const component = new Int32Array(count).fill(-1);
187
+ const stack: number[] = [];
188
+ const frames: number[] = [];
189
+ let nextIndex = 0;
190
+ let componentCount = 0;
191
+ const orRow = (into: number, from: number) => {
192
+ for (let word = 0; word < words; word++) {
193
+ const bits = closed[from * words + word];
194
+ closed[into * words + word] |= word === 0 ? bits & mask : bits;
195
+ }
196
+ };
197
+ const enter = (node: number) => {
198
+ frames.push(node, 0);
199
+ index[node] = lowLink[node] = nextIndex++;
200
+ stack.push(node);
201
+ };
202
+ for (let root = 0; root < count; root++) {
203
+ if (index[root] !== -1) continue;
204
+ enter(root);
205
+ while (frames.length > 0) {
206
+ const node = frames[frames.length - 2];
207
+ const position = frames[frames.length - 1];
208
+ const nodeEdges = edges[node];
209
+ if (position < nodeEdges.length) {
210
+ frames[frames.length - 1] = position + 1;
211
+ const callee = nodeEdges[position];
212
+ if (index[callee] === -1) {
213
+ enter(callee);
214
+ } else if (component[callee] === -1) {
215
+ // A visited node without a component is still on the stack.
216
+ lowLink[node] = Math.min(lowLink[node], index[callee]);
217
+ }
218
+ continue;
219
+ }
220
+ frames.length -= 2;
221
+ if (frames.length > 0) {
222
+ const caller = frames[frames.length - 2];
223
+ lowLink[caller] = Math.min(lowLink[caller], lowLink[node]);
224
+ }
225
+ if (lowLink[node] !== index[node]) continue;
226
+ // The component's members share one row, which ors in their callees' rows.
227
+ const members: number[] = [];
228
+ for (;;) {
229
+ const member = stack.pop() ?? node;
230
+ component[member] = componentCount;
231
+ members.push(member);
232
+ if (member === node) break;
233
+ }
234
+ componentCount++;
235
+ const first = members[0];
236
+ members.forEach((member) => {
237
+ if (member !== first) orRow(first, member);
238
+ edges[member].forEach((callee) => {
239
+ if (component[callee] !== component[first]) orRow(first, callee);
240
+ });
241
+ });
242
+ members.forEach((member) => {
243
+ if (member !== first) orRow(member, first);
244
+ });
245
+ }
246
+ }
247
+ return closed;
248
+ };
249
+
250
+ const PURITY_MASK = IMPURE | REPEATED_EFFECT | STREAMED;
251
+ const READ_MASK = NONCONSTANT | READS_IMPURE;
252
+
253
+ // TLC evaluates a constant definition without parameters once, so only other definitions pass
254
+ // on the side effects and random choices of the builtins they call.
255
+ const isEvaluatedOnce = (definition: Definition, flags: number) =>
256
+ definition.kind === "Operator" && definition.params.length === 0 && (flags & NONCONSTANT) === 0;
257
+
258
+ export const createDefinitionFacts = (environment: DefinitionNames): DefinitionFacts => {
259
+ const definitions = [...new Set(environment.definitions.values())];
260
+ const ids = new Map(definitions.map((definition, id) => [definition, id]));
261
+ const words = 1 + ((environment.variables.length + 31) >>> 5);
262
+ const local = new Uint32Array(definitions.length * words);
263
+ const localImpurity = new Uint32Array(definitions.length * words);
264
+ const edges = definitions.map((definition, id) => {
265
+ const facts = collectLocalFacts(getDefinitionExpressions(definition), environment, words);
266
+ local.set(facts.row, id * words);
267
+ localImpurity[id * words] = facts.row[0] & PURITY_MASK;
268
+ return facts.edges;
269
+ });
270
+ const getId = (edge: Edge) => ids.get(edge.callee) ?? -1;
271
+ const readEdges = edges.map(
272
+ (own) => new Int32Array(own.filter((edge) => !edge.isOverridden).map(getId)),
273
+ );
274
+ const reads = closeRows(local, words, readEdges, READ_MASK);
275
+ const levelEdges = edges.map(
276
+ (own) =>
277
+ new Int32Array(own.filter((edge) => !edge.isOverridden && edge.isLevelCounted).map(getId)),
278
+ );
279
+ const levels = closeRows(local, words, levelEdges, TEMPORAL_LEVEL_FLAGS);
280
+ const purityEdges = edges.map(
281
+ (own) =>
282
+ new Int32Array(
283
+ own
284
+ .map(getId)
285
+ .filter((callee) => !isEvaluatedOnce(definitions[callee], reads[callee * words])),
286
+ ),
287
+ );
288
+ const purity = closeRows(localImpurity, words, purityEdges, PURITY_MASK);
289
+ const rows = reads.map((word, position) =>
290
+ position % words === 0
291
+ ? (word & READ_MASK) |
292
+ (levels[position] & TEMPORAL_LEVEL_FLAGS) |
293
+ (purity[position] & PURITY_MASK)
294
+ : word,
295
+ );
296
+ return { ids, rows, words };
297
+ };
298
+
299
+ const getRow = (definition: Definition, environment: Environment): Uint32Array => {
300
+ const facts = environment.facts;
301
+ const id = facts.ids.get(definition);
302
+ return id === undefined
303
+ ? getExpressionsRow(getDefinitionExpressions(definition), environment)
304
+ : facts.rows.subarray(id * facts.words, (id + 1) * facts.words);
305
+ };
306
+
307
+ const getExpressionsRow = (expressions: Expression[], environment: Environment) => {
308
+ const words = environment.facts.words;
309
+ const local = collectLocalFacts(expressions, environment, words);
310
+ const row = local.row;
311
+ local.edges.forEach((edge) => {
312
+ const callee = getRow(edge.callee, environment);
313
+ if (!edge.isOverridden) {
314
+ row[0] |= callee[0] & READ_MASK;
315
+ if (edge.isLevelCounted) row[0] |= callee[0] & TEMPORAL_LEVEL_FLAGS;
316
+ for (let word = 1; word < words; word++) row[word] |= callee[word];
317
+ }
318
+ if (!isEvaluatedOnce(edge.callee, callee[0])) row[0] |= callee[0] & PURITY_MASK;
319
+ });
320
+ return row;
321
+ };
322
+
323
+ export const isConstantLevel = (definition: Definition, environment: Environment) =>
324
+ (getRow(definition, environment)[0] & NONCONSTANT) === 0;
325
+
326
+ const EFFECT_FLAGS = { any: IMPURE, repeated: REPEATED_EFFECT, streamed: STREAMED };
327
+
328
+ export const isImpureDefinition = (
329
+ definition: Definition,
330
+ environment: Environment,
331
+ effect: keyof typeof EFFECT_FLAGS = "any",
332
+ ) => (getRow(definition, environment)[0] & EFFECT_FLAGS[effect]) !== 0;
333
+
334
+ export const getDefinitionLevel = (definition: Definition, environment: Environment) => {
335
+ const flags = getRow(definition, environment)[0] & TEMPORAL_LEVEL_FLAGS;
336
+ return flags === TEMPORAL_LEVEL_FLAGS
337
+ ? TEMPORAL_LEVEL
338
+ : flags === ACTION_LEVEL_FLAGS
339
+ ? ACTION_LEVEL
340
+ : flags === VARIABLE_LEVEL_FLAGS
341
+ ? VARIABLE_LEVEL
342
+ : CONSTANT_LEVEL;
343
+ };
344
+
345
+ const toVariables = (row: Uint32Array) => {
346
+ const variables: number[] = [];
347
+ for (let word = 1; word < row.length; word++) {
348
+ for (let bits = row[word]; bits !== 0; bits &= bits - 1) {
349
+ variables.push(((word - 1) << 5) + (31 - Math.clz32(bits & -bits)));
350
+ }
351
+ }
352
+ return variables;
353
+ };
354
+
355
+ // Undefined when the expression has an effect or is an action.
356
+ export const getReadVariables = (
357
+ expression: Expression,
358
+ environment: Environment,
359
+ ): number[] | undefined => {
360
+ const row = getExpressionsRow([expression], environment);
361
+ return row[0] & READS_IMPURE ? undefined : toVariables(row);
362
+ };
363
+
364
+ export const isNonconstant = (expression: Expression, environment: Environment) =>
365
+ (getExpressionsRow([expression], environment)[0] & NONCONSTANT) !== 0;