tla-ts 0.0.3-dev.e5b05b9 → 0.0.3-dev.e8bef80

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 (79) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +150 -48
  5. package/dist/index.d.ts +150 -48
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -22
  12. package/dist/run-cli.js +1352 -23
  13. package/dist/transfer.cjs +83 -43
  14. package/dist/transfer.js +83 -43
  15. package/package.json +5 -1
  16. package/src/ast.ts +28 -4
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -17
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1317 -428
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +226 -207
  25. package/src/environment.ts +221 -75
  26. package/src/errors.ts +130 -15
  27. package/src/evaluate.ts +713 -259
  28. package/src/exit-codes.ts +6 -24
  29. package/src/fingerprints.ts +1 -2
  30. package/src/index.ts +1 -0
  31. package/src/instances.ts +3 -1
  32. package/src/java-overrides.ts +104 -25
  33. package/src/levels.ts +6 -1
  34. package/src/lexer.ts +122 -29
  35. package/src/live-check.ts +204 -148
  36. package/src/liveness.ts +191 -122
  37. package/src/obligation-expression.ts +2 -2
  38. package/src/obligation-source.ts +2 -0
  39. package/src/parallel-worker.ts +6 -1
  40. package/src/parallel.ts +57 -6
  41. package/src/parser.ts +327 -147
  42. package/src/pcal-ast.ts +770 -0
  43. package/src/pcal-parse.ts +1523 -0
  44. package/src/pcal-tokenize.ts +516 -0
  45. package/src/pcal-translate.ts +1230 -0
  46. package/src/pcal-validate.ts +454 -0
  47. package/src/randomization.ts +24 -7
  48. package/src/resolve.ts +252 -87
  49. package/src/run-cli.ts +1162 -200
  50. package/src/runtime.ts +16 -0
  51. package/src/sany-builtins.ts +147 -0
  52. package/src/sany-generator.ts +3128 -0
  53. package/src/sany-levels.ts +1208 -0
  54. package/src/sany-lexer.ts +410 -480
  55. package/src/sany-linter.ts +122 -0
  56. package/src/sany-modules.ts +1398 -0
  57. package/src/sany-nodes.ts +463 -0
  58. package/src/sany-parse-error.ts +2033 -3015
  59. package/src/sany-report.ts +356 -0
  60. package/src/sany-selectors.ts +738 -0
  61. package/src/sany-source.ts +248 -0
  62. package/src/sany-xml.ts +1017 -0
  63. package/src/spec-description.ts +25 -5
  64. package/src/standard-modules.ts +172 -129
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +121 -26
  67. package/src/successors.ts +141 -173
  68. package/src/symmetry.ts +168 -50
  69. package/src/tableau.ts +3 -1
  70. package/src/tlc-ext.ts +13 -17
  71. package/src/tlc-messages.ts +329 -0
  72. package/src/tlc-module.ts +49 -21
  73. package/src/tlc-modules.ts +0 -4
  74. package/src/tlc-order.ts +55 -9
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/values.ts +664 -317
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.3-dev.e5b05b9",
3
+ "version": "0.0.3-dev.e8bef80",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
@@ -54,6 +54,9 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  },
57
+ "dependencies": {
58
+ "fflate": "^0.8.3"
59
+ },
57
60
  "devDependencies": {
58
61
  "@types/node": "^22",
59
62
  "publint": "^0.3.0",
@@ -71,6 +74,7 @@
71
74
  "conformance:sany": "tsx scripts/proof-conformance.ts",
72
75
  "conformance:record": "tsx scripts/record-tlc.ts",
73
76
  "conformance:record-sany": "tsx scripts/record-sany.ts",
77
+ "conformance:record-statechart": "tsx scripts/record-statechart.ts",
74
78
  "conformance:record-tlapm": "tsx scripts/record-tlapm.ts",
75
79
  "conformance:tlapm": "tsx scripts/tlapm-conformance.ts",
76
80
  "publint": "publint",
package/src/ast.ts CHANGED
@@ -34,23 +34,28 @@ export interface StringExpression {
34
34
  }
35
35
 
36
36
  // selectorRanges holds the range of each part of a name such as foo!lab, where SANY reports an
37
- // error in selecting that part.
37
+ // error in selecting that part. spelling holds the text of an operator written as a synonym.
38
38
  export interface IdentifierExpression {
39
39
  kind: "Identifier";
40
40
  name: string;
41
+ spelling?: string;
41
42
  selectorRanges?: SourceRange[];
42
43
  location: SourceLocation;
43
44
  resolution?: LocalResolution;
44
45
  }
45
46
 
47
+ // spelling holds the text of an operator written as a synonym, such as (+) for \oplus, which SANY
48
+ // reports as written.
46
49
  export interface ApplyExpression {
47
50
  kind: "Apply";
48
51
  operator: string;
52
+ spelling?: string;
49
53
  args: Expression[];
50
54
  // The number of args each part of a name like F(x)!l(y) takes, when a part before the last takes
51
55
  // some.
52
56
  argumentCounts?: number[];
53
- // The range of the parenthesized arguments of a named operator, where SANY reports their count.
57
+ // The range of the parenthesized arguments of a named operator, or of the whole name when its
58
+ // last part takes none, where SANY reports their count.
54
59
  argumentsRange?: SourceRange;
55
60
  selectorRanges?: SourceRange[];
56
61
  // The range of the arguments of each part of a name like F(x)!l(y), for a part that takes some.
@@ -62,14 +67,18 @@ export interface ApplyExpression {
62
67
  export interface OperatorReferenceExpression {
63
68
  kind: "OperatorReference";
64
69
  operator: string;
70
+ spelling?: string;
65
71
  location: SourceLocation;
66
72
  }
67
73
 
74
+ // operator holds the operator, such as SUBSET, that an argument names, which tla-ts reads as a
75
+ // LAMBDA.
68
76
  export interface LambdaExpression {
69
77
  kind: "Lambda";
70
78
  params: string[];
71
79
  paramRanges?: SourceRange[];
72
80
  body: Expression;
81
+ operator?: string;
73
82
  location: SourceLocation;
74
83
  }
75
84
 
@@ -77,6 +86,9 @@ export interface JunctionExpression {
77
86
  kind: "And" | "Or";
78
87
  operands: Expression[];
79
88
  isBulleted?: boolean;
89
+ // SANY parses an infix chain such as A /\ B /\ C as (A /\ B) /\ C, whose node of each prefix of
90
+ // two or more operands ends after the parentheses of its last operand.
91
+ prefixEnds?: SourceLocation[];
80
92
  location: SourceLocation;
81
93
  }
82
94
 
@@ -242,6 +254,14 @@ export interface FunctionApplicationExpression {
242
254
  location: SourceLocation;
243
255
  }
244
256
 
257
+ // SANY parses [e1, ..., en] as a function application without the function, and its semantic
258
+ // checker rejects it.
259
+ export interface BracketExpression {
260
+ kind: "Bracket";
261
+ args: Expression[];
262
+ location: SourceLocation;
263
+ }
264
+
245
265
  export interface FieldAccessExpression {
246
266
  kind: "FieldAccess";
247
267
  record: Expression;
@@ -328,6 +348,7 @@ export type Expression =
328
348
  | ExceptExpression
329
349
  | ExceptAtExpression
330
350
  | FunctionApplicationExpression
351
+ | BracketExpression
331
352
  | FieldAccessExpression
332
353
  | ProductExpression
333
354
  | TemporalExpression
@@ -360,6 +381,8 @@ export interface FunctionDefinition {
360
381
  body: Expression;
361
382
  isLocal: boolean;
362
383
  isRecursive: boolean;
384
+ // Whether the body applies the function, which SANY makes a recursive function definition.
385
+ isSelfReferential?: boolean;
363
386
  location: SourceLocation;
364
387
  }
365
388
 
@@ -488,8 +511,7 @@ export interface Theorem {
488
511
 
489
512
  // The units of a module in source order, for consumers that need the order across kinds.
490
513
  export type ModuleUnit =
491
- | { kind: "Constants"; declarations: Declaration[] }
492
- | { kind: "Variables"; declarations: Declaration[] }
514
+ | { kind: "Constants" | "Variables"; declarations: Declaration[]; location: SourceLocation }
493
515
  | { kind: "Recursive"; declarations: Declaration[] }
494
516
  | { kind: "Definition"; definition: Definition }
495
517
  | { kind: "Instance"; instance: Instance }
@@ -583,6 +605,8 @@ export const getChildren = (expression: Expression): Expression[] => {
583
605
  ];
584
606
  case "FunctionApplication":
585
607
  return [expression.function, ...expression.args];
608
+ case "Bracket":
609
+ return expression.args;
586
610
  case "FieldAccess":
587
611
  return [expression.record];
588
612
  case "Product":
package/src/bags.ts CHANGED
@@ -123,7 +123,8 @@ const mergeBags = (bags: FunctionValue[], method: string) =>
123
123
 
124
124
  const EMPTY_BAG = createSortedFunction([], []);
125
125
 
126
- export const BAGS_OPERATORS: Builtin[] = [
126
+ // SubBag compares the counts with the >= of Naturals, which Integers replaces when TLC loads it.
127
+ export const createBagsOperators = (builtins: ReadonlyMap<string, Builtin>): Builtin[] => [
127
128
  defineConstant("EmptyBag", EMPTY_BAG),
128
129
  defineOperator("IsABag", 1, (args) => {
129
130
  const functionValue = toFunction(getValue(args, 0, "IsABag"));
@@ -228,12 +229,8 @@ export const BAGS_OPERATORS: Builtin[] = [
228
229
  const functionValue = toFiniteFunction(value);
229
230
  const range = createSet(functionValue.range).elements;
230
231
  const invalid = range.find((count) => typeof count !== "number");
231
- if (invalid !== undefined) {
232
- const isFirst = typeof range[0] !== "number";
233
- throw new TlaEvaluationError(
234
- `The ${isFirst ? "first" : "second"} argument of >= should be an integer, but instead it is:\n${formatValue(isFirst ? range[0] : invalid)}`,
235
- );
236
- }
232
+ // A count that isn't an integer fails the comparison.
233
+ if (invalid !== undefined) builtins.get(">=")?.evaluateBinary?.(range[0], invalid);
237
234
  const counts = functionValue.range.filter((count) => typeof count === "number");
238
235
  if (counts.some((count) => count < 1)) {
239
236
  throw new TlaEvaluationError(
package/src/builtins.ts CHANGED
@@ -2,6 +2,7 @@ import { TlaEvaluationError, TlaModuleError } from "./errors";
2
2
  import type { Builtin, BuiltinArgument, Context, OperatorValue } from "./evaluate";
3
3
  import {
4
4
  formatValue,
5
+ getFunctionClass,
5
6
  getSequenceElements,
6
7
  isEnumerable,
7
8
  isFunctionValue,
@@ -9,6 +10,7 @@ import {
9
10
  toFiniteFunction,
10
11
  MAXIMUM_INTEGER,
11
12
  MINIMUM_INTEGER,
13
+ setTrace,
12
14
  toFiniteSet,
13
15
  type Value,
14
16
  } from "./values";
@@ -243,17 +245,20 @@ const KIND_STRINGS: Record<string, string> = {
243
245
  UnionValue: "a set of the form UNION S",
244
246
  };
245
247
 
246
- // TLC stores a function as a TupleValue or a RecordValue when it has that shape, so the
247
- // shape stands in for how TLC built the value.
248
+ const FUNCTION_CLASSES = {
249
+ lambda: "FcnLambdaValue",
250
+ tuple: "TupleValue",
251
+ record: "RecordValue",
252
+ };
253
+
248
254
  export const getJavaClassName = (value: Value) => {
249
255
  if (typeof value === "number") return "IntValue";
250
256
  if (typeof value === "string") return "StringValue";
251
257
  if (typeof value === "boolean") return "BoolValue";
252
258
  if (value.kind === "SetOperation") return SET_OPERATION_CLASSES[value.operator];
253
259
  if (value.kind !== "Function") return JAVA_CLASSES[value.kind];
254
- const domain = value.domain;
255
- if (domain.every((key, index) => key === index + 1)) return "TupleValue";
256
- return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
260
+ const functionClass = getFunctionClass(value);
261
+ return functionClass === undefined ? "FcnRcdValue" : FUNCTION_CLASSES[functionClass];
257
262
  };
258
263
 
259
264
  export const getKindString = (value: Value) => KIND_STRINGS[getJavaClassName(value)];
@@ -270,13 +275,17 @@ export const overrideWithJava = (builtin: Builtin, signature: string): Builtin =
270
275
  const guard =
271
276
  <Arguments extends unknown[]>(compute: (...args: Arguments) => Value) =>
272
277
  (...args: Arguments) => {
278
+ const depth = setTrace.sets?.length ?? 0;
273
279
  try {
274
280
  return compute(...args);
275
281
  } catch (error) {
276
282
  const isWrapped =
277
283
  error instanceof TlaEvaluationError &&
278
284
  (builtin.isLazy === true || !(error instanceof TlaModuleError));
279
- throw isWrapped ? createJavaOverrideError(signature, error.message) : error;
285
+ if (!isWrapped) throw error;
286
+ // The wrapping error replaces a FingerprintException that a set with a source raised.
287
+ if (setTrace.sets) setTrace.sets.length = depth;
288
+ throw createJavaOverrideError(signature, error.message);
280
289
  }
281
290
  };
282
291
  return {
@@ -297,21 +306,16 @@ export const getJavaInteger = (value: Value, signature: string) => {
297
306
  return value;
298
307
  };
299
308
 
300
- export const getArithmeticSignature = (
301
- javaClass: "Naturals" | "Integers",
302
- method: string,
303
- result: string,
304
- arity: number,
305
- ) =>
306
- `public static tlc2.value.impl.${result} tlc2.module.${javaClass}.${method}(${Array.from({ length: arity }, () => "tlc2.value.impl.IntValue").join(",")})`;
309
+ export const getJavaSignature = (module: string, method: string, result: string, arity: number) =>
310
+ `public static tlc2.value.impl.${result} tlc2.module.${module}.${method}(${Array.from({ length: arity }, () => "tlc2.value.impl.IntValue").join(",")})`;
307
311
 
308
312
  export const defineArithmetic = (
309
- javaClass: "Naturals" | "Integers",
313
+ module: string,
310
314
  name: string,
311
315
  method: string,
312
316
  compute: (left: number, right: number) => number,
313
317
  ) => {
314
- const signature = getArithmeticSignature(javaClass, method, "IntValue", 2);
318
+ const signature = getJavaSignature(module, method, "IntValue", 2);
315
319
  return defineBinary(name, (left, right) => {
316
320
  const leftNumber = getJavaInteger(left, signature);
317
321
  const rightNumber = getJavaInteger(right, signature);
@@ -330,9 +334,17 @@ const getIntegerArgument = (value: Value, position: string, name: string) => {
330
334
  return value;
331
335
  };
332
336
 
333
- export const defineComparison = (name: string, compare: (left: number, right: number) => boolean) =>
337
+ // The error of an argument that isn't an integer names the operator as TLC's method does.
338
+ export const defineComparison = (
339
+ name: string,
340
+ compare: (left: number, right: number) => boolean,
341
+ errorName = name,
342
+ ) =>
334
343
  defineBinary(name, (left, right) =>
335
- compare(getIntegerArgument(left, "first", name), getIntegerArgument(right, "second", name)),
344
+ compare(
345
+ getIntegerArgument(left, "first", errorName),
346
+ getIntegerArgument(right, "second", errorName),
347
+ ),
336
348
  );
337
349
 
338
350
  export const defineLazyOperator = (
@@ -1,6 +1,6 @@
1
1
  // TLC's Reals.tla, RealTime.tla, and Toolbox.tla, verbatim, so that errors inside them name the same
2
2
  // lines as TLC's errors.
3
- const REALS = `-------------------------------- MODULE Reals -------------------------------
3
+ export const REALS = `-------------------------------- MODULE Reals -------------------------------
4
4
  (***************************************************************************)
5
5
  (* This module provides dummy definitions of the operators that are *)
6
6
  (* defined by the real Reals module. It is expected that any tool that *)
@@ -19,7 +19,7 @@ a / b == CHOOSE m \\in Real : m * b = a
19
19
  Infinity == CHOOSE i : (i \\notin Real) /\\ (\\A r \\in Real : i > r)
20
20
  =============================================================================`;
21
21
 
22
- const REAL_TIME = `----------------------------- MODULE RealTime -------------------------------
22
+ export const REAL_TIME = `----------------------------- MODULE RealTime -------------------------------
23
23
  (***************************************************************************)
24
24
  (* This standard module is described in Chapter 9 of the book "Specifying *)
25
25
  (* Systems". The type of specification described there cannot be handled *)
@@ -47,7 +47,7 @@ RTnow(v) == LET NowNext == /\\ now' \\in {r \\in Real : r > now}
47
47
  /\\ \\A r \\in Real : WF_now(NowNext /\\ (now'>r))
48
48
  =============================================================================`;
49
49
 
50
- const TOOLBOX = `----------------------------- MODULE Toolbox --------------------------------
50
+ export const TOOLBOX = `----------------------------- MODULE Toolbox --------------------------------
51
51
  (***************************************************************************)
52
52
  (* *)
53
53
  (***************************************************************************)
package/src/canonical.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { getValueHash, isSameValue, type Value } from "./values";
1
+ import { getValueHash, isInterchangeableValue, type Value } from "./values";
2
2
 
3
3
  const canonicalizeElements = (elements: Value[], canonicalize: (value: Value) => Value) => {
4
4
  for (let index = 0; index < elements.length; index++) {
@@ -15,11 +15,12 @@ export const createCanonicalizer = () => {
15
15
  const hash = getValueHash(value);
16
16
  const bucket = canonicalValues.get(hash);
17
17
  if (bucket !== undefined && !Array.isArray(bucket)) {
18
- if (bucket === value || isSameValue(bucket, value)) return bucket;
18
+ if (bucket === value || isInterchangeableValue(bucket, value)) return bucket;
19
19
  }
20
20
  if (Array.isArray(bucket)) {
21
21
  for (let index = 0; index < bucket.length; index++) {
22
- if (bucket[index] === value || isSameValue(bucket[index], value)) return bucket[index];
22
+ if (bucket[index] === value || isInterchangeableValue(bucket[index], value))
23
+ return bucket[index];
23
24
  }
24
25
  }
25
26
  if (value.kind === "Set") canonicalizeElements(value.elements, canonicalize);