tla-ts 0.0.3 → 0.0.4-dev.4819fb7

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 (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +162 -8
  6. package/dist/index.d.ts +162 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +77 -37
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +994 -279
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +200 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +220 -108
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +741 -189
package/src/exit-codes.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { LivenessChecker } from "./liveness";
1
+ import type { LivenessChecker } from "./live-check";
2
2
 
3
3
  // TLC's exit statuses, from EC.ExitStatus.
4
4
  export const EXIT_STATUS = {
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
17
17
  } as const;
18
18
 
19
19
  const exitCodes = new WeakMap<Error, number>();
20
- const moduleExitCodes = new WeakMap<Error, number>();
21
- const phase = { isNextState: false };
22
-
23
- export const setNextStatePhase = (isNextState: boolean) => {
24
- phase.isNextState = isNextState;
25
- };
26
20
 
27
21
  // The innermost site that knows TLC's exit status for an error sets it first and wins.
28
22
  export const setExitCode = <Thrown>(error: Thrown, code: number) => {
@@ -30,46 +24,31 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
30
24
  return error;
31
25
  };
32
26
 
33
- // TLC exits with the code of an exception from a standard module's Java code only when the exception
34
- // escapes the computation of next states. In every other phase, the phase decides the code.
35
- export const setModuleExitCode = (error: Error, code: number) => {
36
- if (phase.isNextState) moduleExitCodes.set(error, code);
37
- };
38
-
39
- export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
27
+ export const getExitCode = (error: Error) => exitCodes.get(error);
40
28
 
41
29
  // TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
42
- export const withLivenessExitCode = (
43
- create: () => LivenessChecker | undefined,
44
- ): LivenessChecker | undefined => {
30
+ const withErrorExitCode = <Result>(run: () => Result) => {
45
31
  try {
46
- const checker = create();
47
- return (
48
- checker && {
49
- addInitialState: (index, state) => {
50
- try {
51
- checker.addInitialState(index, state);
52
- } catch (error) {
53
- throw setExitCode(error, EXIT_STATUS.error);
54
- }
55
- },
56
- addState: (index, state, successors) => {
57
- try {
58
- return checker.addState(index, state, successors);
59
- } catch (error) {
60
- throw setExitCode(error, EXIT_STATUS.error);
61
- }
62
- },
63
- findViolation: () => {
64
- try {
65
- return checker.findViolation();
66
- } catch (error) {
67
- throw setExitCode(error, EXIT_STATUS.error);
68
- }
69
- },
70
- }
71
- );
32
+ return run();
72
33
  } catch (error) {
73
34
  throw setExitCode(error, EXIT_STATUS.error);
74
35
  }
75
36
  };
37
+
38
+ export const withLivenessExitCode = (
39
+ create: () => LivenessChecker | undefined,
40
+ ): LivenessChecker | undefined => {
41
+ const checker = withErrorExitCode(create);
42
+ return (
43
+ checker && {
44
+ branches: checker.branches,
45
+ addInitialState: (index, state, tracing) =>
46
+ withErrorExitCode(() => checker.addInitialState(index, state, tracing)),
47
+ addState: (index, state, successors, tracing) =>
48
+ withErrorExitCode(() => checker.addState(index, state, successors, tracing)),
49
+ findViolation: () => withErrorExitCode(() => checker.findViolation()),
50
+ getViolatedProperties: (path, cycleStart) =>
51
+ withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
52
+ }
53
+ );
54
+ };
@@ -1,12 +1,10 @@
1
1
  import { getValueFingerprint, getValueHash, type Value } from "./values";
2
2
 
3
3
  const EMPTY = 0;
4
- const TOMBSTONE = 1;
5
4
  const INITIAL_CAPACITY = 1 << 12;
6
5
 
7
6
  interface FingerprintTable {
8
7
  insert: (first: number, second: number) => boolean;
9
- remove: (first: number, second: number) => void;
10
8
  }
11
9
 
12
10
  const mix = (hash: number) => {
@@ -26,15 +24,13 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
26
24
  fingerprint[1] = mix(second);
27
25
  };
28
26
 
29
- export const getFingerprintOwner = (second: number, count: number) => (second >>> 0) % count;
30
-
31
27
  export const createFingerprintTable = (): FingerprintTable => {
32
28
  const table = { slots: new Int32Array(INITIAL_CAPACITY * 2), used: 0 };
33
29
  const findSlot = (slots: Int32Array, first: number, second: number) => {
34
30
  const mask = slots.length / 2 - 1;
35
31
  for (let index = first & mask; ; index = (index + 1) & mask) {
36
32
  const stored = slots[index * 2];
37
- if (stored === EMPTY && slots[index * 2 + 1] === EMPTY) return index;
33
+ if (stored === EMPTY) return index;
38
34
  if (stored === first && slots[index * 2 + 1] === second) return index;
39
35
  }
40
36
  };
@@ -59,11 +55,50 @@ export const createFingerprintTable = (): FingerprintTable => {
59
55
  table.used++;
60
56
  return true;
61
57
  },
62
- remove: (first, second) => {
63
- const slot = findSlot(table.slots, first, second);
64
- if (table.slots[slot * 2] !== first) return;
65
- table.slots[slot * 2] = EMPTY;
66
- table.slots[slot * 2 + 1] = TOMBSTONE;
67
- },
68
58
  };
69
59
  };
60
+
61
+ // Workers share one table of 64-bit fingerprints. The first word is never 0, so 0 marks an empty slot.
62
+ export interface SharedFingerprints {
63
+ slots: BigInt64Array;
64
+ used: Int32Array;
65
+ }
66
+
67
+ const INITIAL_SHARED_CAPACITY = 1 << 10;
68
+
69
+ const insertSharedKey = (slots: BigInt64Array, key: bigint) => {
70
+ const mask = slots.length - 1;
71
+ for (let index = Number(key >> 32n) & mask; ; index = (index + 1) & mask) {
72
+ const stored = Atomics.load(slots, index);
73
+ if (stored === key) return false;
74
+ if (stored !== 0n) continue;
75
+ const previous = Atomics.compareExchange(slots, index, 0n, key);
76
+ if (previous === 0n) return true;
77
+ if (previous === key) return false;
78
+ }
79
+ };
80
+
81
+ export const createSharedFingerprints = (): SharedFingerprints => ({
82
+ slots: new BigInt64Array(new SharedArrayBuffer(INITIAL_SHARED_CAPACITY * 8)),
83
+ used: new Int32Array(new SharedArrayBuffer(4)),
84
+ });
85
+
86
+ export const insertSharedFingerprint = (
87
+ table: SharedFingerprints,
88
+ first: number,
89
+ second: number,
90
+ ) => {
91
+ const key = BigInt.asIntN(64, (BigInt(first) << 32n) | BigInt(second >>> 0));
92
+ if (!insertSharedKey(table.slots, key)) return false;
93
+ Atomics.add(table.used, 0, 1);
94
+ return true;
95
+ };
96
+
97
+ export const isSharedFingerprintsFull = (table: SharedFingerprints) =>
98
+ Atomics.load(table.used, 0) * 2 >= table.slots.length;
99
+
100
+ export const growSharedFingerprints = (table: SharedFingerprints): SharedFingerprints => {
101
+ const slots = new BigInt64Array(new SharedArrayBuffer(table.slots.byteLength * 2));
102
+ for (const key of table.slots) if (key !== 0n) insertSharedKey(slots, key);
103
+ return { slots, used: table.used };
104
+ };
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export type {
2
2
  CheckOptions,
3
+ CheckProgress,
3
4
  CheckResult,
4
5
  CheckStatus,
5
6
  EvaluationFailure,
package/src/instances.ts CHANGED
@@ -1,16 +1,17 @@
1
- import type {
2
- Bound,
3
- BoundPattern,
4
- Definition,
5
- Expression,
6
- Instance,
7
- Module,
8
- OperatorParam,
9
- SourceLocation,
10
- Theorem,
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Bound,
5
+ type BoundPattern,
6
+ type Definition,
7
+ type Expression,
8
+ type Instance,
9
+ type Module,
10
+ type OperatorParam,
11
+ type SourceLocation,
12
+ type Theorem,
11
13
  } from "./ast";
12
14
  import { TlaSyntaxError } from "./errors";
13
- import { getChildren, isLocallyResolved } from "./evaluate";
14
15
  import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
15
16
 
16
17
  export const substitutedExpressions = new WeakSet<Expression>();
@@ -163,7 +164,7 @@ export const mapChildren = (
163
164
  case "RecordSet":
164
165
  return {
165
166
  ...expression,
166
- fields: expression.fields.map((field) => ({ name: field.name, value: plain(field.value) })),
167
+ fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
167
168
  };
168
169
  case "Except":
169
170
  return {
@@ -182,6 +183,8 @@ export const mapChildren = (
182
183
  function: plain(expression.function),
183
184
  args: expression.args.map(plain),
184
185
  };
186
+ case "Bracket":
187
+ return { ...expression, args: expression.args.map(plain) };
185
188
  case "FieldAccess":
186
189
  return { ...expression, record: plain(expression.record) };
187
190
  case "Product":
@@ -218,7 +221,7 @@ interface Instantiation {
218
221
  variables: Set<string>;
219
222
  getDefinition: (key: string) => Definition | undefined;
220
223
  isParameterized: (key: string) => boolean;
221
- addDefinition: (name: string, create: (key: string) => Definition) => string;
224
+ addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
222
225
  createName: (name: string) => string;
223
226
  }
224
227
 
@@ -639,7 +642,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
639
642
  const instantiation = rewrite.instantiation;
640
643
  const definition = instantiation.getDefinition(key);
641
644
  if (!definition) return key;
642
- return instantiation.addDefinition(definition.name, (copyKey) => {
645
+ return instantiation.addDefinition(key, definition.name, (copyKey) => {
643
646
  rewrite.copies.set(key, copyKey);
644
647
  const rename = createRenamer(rewrite);
645
648
  const mapped = renameParams(
@@ -678,7 +681,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
678
681
  if (copy !== undefined) return copy;
679
682
  const definition = instantiation.getDefinition(key);
680
683
  if (!definition) return key;
681
- return instantiation.addDefinition(definition.name, () =>
684
+ return instantiation.addDefinition(key, definition.name, () =>
682
685
  withInstanceParams(definition, instantiation.params, definition.location),
683
686
  );
684
687
  },
@@ -704,7 +707,7 @@ const expandLetInstances = (
704
707
  const index = position === -1 ? current.length : position;
705
708
  return [...current.slice(0, index), ...instantiate(instance, inner), ...current.slice(index)];
706
709
  }, mapped.definitions);
707
- return { kind: "Let", definitions, body: mapped.body, location: mapped.location };
710
+ return { ...mapped, definitions };
708
711
  };
709
712
 
710
713
  // A named theorem's statement is linked as its definition.
@@ -12,9 +12,20 @@ interface PoolEntry {
12
12
  interface JavaMethod {
13
13
  name: string;
14
14
  arity: number;
15
+ // Method#toString, which TLC prints as the signature of an override.
16
+ signature: string;
15
17
  code: Uint8Array | undefined;
16
18
  }
17
19
 
20
+ // A public static method of the class named after a module, which TLC reports as it loads the
21
+ // class, with whether it overrides an operator.
22
+ export interface JavaOverride {
23
+ module: string;
24
+ operator: string;
25
+ signature: string;
26
+ isMatched: boolean;
27
+ }
28
+
18
29
  interface ObjectReference {
19
30
  className: string;
20
31
  value: Expression | undefined;
@@ -44,12 +55,44 @@ const createReader = (bytes: Uint8Array) => {
44
55
  };
45
56
  };
46
57
 
47
- const getParameterCount = (descriptor: string) => {
48
- const parameters = descriptor.slice(1, descriptor.indexOf(")"));
49
- return [...parameters.matchAll(/\[*(?:L[^;]*;|[BCDFIJSZ])/g)].length;
50
- };
58
+ const PRIMITIVE_TYPES = new Map([
59
+ ["B", "byte"],
60
+ ["C", "char"],
61
+ ["D", "double"],
62
+ ["F", "float"],
63
+ ["I", "int"],
64
+ ["J", "long"],
65
+ ["S", "short"],
66
+ ["Z", "boolean"],
67
+ ["V", "void"],
68
+ ]);
69
+
70
+ // The modifiers Method#toString prints, in the order of Modifier#toString.
71
+ const METHOD_MODIFIERS: [number, string][] = [
72
+ [0x0001, "public"],
73
+ [0x0004, "protected"],
74
+ [0x0002, "private"],
75
+ [0x0400, "abstract"],
76
+ [0x0008, "static"],
77
+ [0x0010, "final"],
78
+ [0x0020, "synchronized"],
79
+ [0x0100, "native"],
80
+ [0x0800, "strictfp"],
81
+ ];
82
+
83
+ const getTypeNames = (descriptor: string) =>
84
+ [...descriptor.matchAll(/(\[*)(?:L([^;]*);|([BCDFIJSZV]))/g)].map(
85
+ (match) =>
86
+ `${match[2]?.replaceAll("/", ".") ?? PRIMITIVE_TYPES.get(match[3])}${"[]".repeat(match[1].length)}`,
87
+ );
51
88
 
52
- // The public static methods of a class file and their bytecode, as java.lang.Class lists them.
89
+ const getClassName = (pool: PoolEntry[], index: number) => pool[pool[index].first].text;
90
+
91
+ const getJavaClassName = (pool: PoolEntry[], index: number) =>
92
+ getClassName(pool, index).replaceAll("/", ".");
93
+
94
+ // The public static methods of a class file and their bytecode, in the order of the class file.
95
+ // Class#getDeclaredMethods orders them by where HotSpot allocated the symbols of their names.
53
96
  const readMethods = (bytes: Uint8Array) => {
54
97
  const reader = createReader(bytes);
55
98
  if (reader.readUint32() !== CLASS_MAGIC)
@@ -80,7 +123,7 @@ const readMethods = (bytes: Uint8Array) => {
80
123
  }
81
124
  }
82
125
  reader.readUint16();
83
- reader.readUint16();
126
+ const className = getJavaClassName(pool, reader.readUint16());
84
127
  reader.readUint16();
85
128
  reader.readBytes(reader.readUint16() * 2);
86
129
  for (let fields = reader.readUint16(); fields > 0; fields--) {
@@ -95,23 +138,38 @@ const readMethods = (bytes: Uint8Array) => {
95
138
  const access = reader.readUint16();
96
139
  const name = pool[reader.readUint16()].text;
97
140
  const descriptor = pool[reader.readUint16()].text;
98
- const method: JavaMethod = { name, arity: getParameterCount(descriptor), code: undefined };
99
- for (let attributes = reader.readUint16(); attributes > 0; attributes--) {
100
- const attributeName = pool[reader.readUint16()].text;
101
- const attribute = reader.readBytes(reader.readUint32());
102
- if (attributeName === "Code") {
103
- const length = new DataView(attribute.buffer, attribute.byteOffset + 4, 4).getUint32(0);
104
- method.code = attribute.subarray(8, 8 + length);
105
- }
106
- }
141
+ const parametersEnd = descriptor.indexOf(")");
142
+ const parameters = getTypeNames(descriptor.slice(1, parametersEnd));
143
+ const attributes = Array.from({ length: reader.readUint16() }, () => ({
144
+ name: pool[reader.readUint16()].text,
145
+ reader: createReader(reader.readBytes(reader.readUint32())),
146
+ }));
107
147
  const isOverride = (access & ACCESS_PUBLIC) !== 0 && (access & ACCESS_STATIC) !== 0;
108
- if (isOverride) methods.push(method);
148
+ if (!isOverride) continue;
149
+ const code = attributes.find((attribute) => attribute.name === "Code")?.reader;
150
+ // A Code attribute holds max_stack and max_locals before the length of the bytecode.
151
+ code?.readBytes(4);
152
+ const thrown = attributes.find((attribute) => attribute.name === "Exceptions")?.reader;
153
+ const exceptions = thrown
154
+ ? Array.from({ length: thrown.readUint16() }, () =>
155
+ getJavaClassName(pool, thrown.readUint16()),
156
+ )
157
+ : [];
158
+ const modifiers = METHOD_MODIFIERS.filter(([flag]) => (access & flag) !== 0).map(
159
+ ([, modifier]) => modifier,
160
+ );
161
+ const returnType = getTypeNames(descriptor.slice(parametersEnd + 1))[0];
162
+ const throwsClause = exceptions.length === 0 ? "" : ` throws ${exceptions.join(",")}`;
163
+ methods.push({
164
+ name,
165
+ arity: parameters.length,
166
+ code: code?.readBytes(code.readUint32()),
167
+ signature: `${modifiers.join(" ")} ${returnType} ${className}.${name}(${parameters.join(",")})${throwsClause}`,
168
+ });
109
169
  }
110
170
  return { pool, methods };
111
171
  };
112
172
 
113
- const getClassName = (pool: PoolEntry[], index: number) => pool[pool[index].first].text;
114
-
115
173
  const getMember = (pool: PoolEntry[], index: number) => {
116
174
  const reference = pool[index];
117
175
  const nameAndType = pool[reference.second];
@@ -220,16 +278,29 @@ const getReturnedLiteral = (
220
278
  throw unsupported();
221
279
  };
222
280
 
223
- // TLC loads a class named after each module and replaces every operator the module defines
224
- // with the public static method of the same name and arity.
225
- export const overrideDefinitions = (module: Module, bytes: Uint8Array | undefined): Module => {
226
- if (!bytes) return module;
281
+ // TLC loads a class named after each module and replaces each operator of the module that the
282
+ // root module sees with the public static method of the same name and arity. It reports whether
283
+ // each method matched an operator.
284
+ export const overrideDefinitions = (
285
+ module: Module,
286
+ bytes: Uint8Array | undefined,
287
+ isRoot: boolean,
288
+ ): { module: Module; overrides: JavaOverride[] } => {
289
+ if (!bytes) return { module, overrides: [] };
227
290
  const classFile = readMethods(bytes);
291
+ const arities = new Map(
292
+ module.definitions
293
+ .filter((definition) => isRoot || !definition.isLocal)
294
+ .map((definition) => [
295
+ definition.name,
296
+ definition.kind === "Operator" ? definition.params.length : 0,
297
+ ]),
298
+ );
299
+ const isMatched = (method: JavaMethod) => arities.get(method.name) === method.arity;
228
300
  const definitions = module.definitions.map((definition): Definition => {
229
301
  if (definition.kind !== "Operator") return definition;
230
302
  const method = classFile.methods.find(
231
- (candidate) =>
232
- candidate.name === definition.name && candidate.arity === definition.params.length,
303
+ (candidate) => candidate.name === definition.name && isMatched(candidate),
233
304
  );
234
305
  if (!method) return definition;
235
306
  return {
@@ -237,5 +308,13 @@ export const overrideDefinitions = (module: Module, bytes: Uint8Array | undefine
237
308
  body: getReturnedLiteral(method, classFile.pool, module.name, definition.location),
238
309
  };
239
310
  });
240
- return { ...module, definitions };
311
+ return {
312
+ module: { ...module, definitions },
313
+ overrides: classFile.methods.map((method) => ({
314
+ module: module.name,
315
+ operator: method.name,
316
+ signature: method.signature,
317
+ isMatched: isMatched(method),
318
+ })),
319
+ };
241
320
  };