tla-ts 0.0.3 → 0.0.4-dev.2be6058

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 +160 -8
  6. package/dist/index.d.ts +160 -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 +797 -246
  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 +219 -107
  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 +731 -184
@@ -2,6 +2,8 @@ export interface OptionSpec {
2
2
  flag: string;
3
3
  isRequired?: boolean;
4
4
  isRepeatable?: boolean;
5
+ // How many of the arguments after the flag the option takes, when that depends on them.
6
+ getArity?: (rest: string[]) => number;
5
7
  }
6
8
 
7
9
  export interface ParsedOption {
@@ -29,27 +31,31 @@ export const defineCommand = (usage: string, specs: OptionSpec[]) => {
29
31
  const arities = new Map(
30
32
  specs.map((spec) => {
31
33
  const [name, ...values] = spec.flag.split(" ");
32
- return [name, values.length];
34
+ return [name, spec.getArity ?? (() => values.length)];
33
35
  }),
34
36
  );
35
37
  const parse = (args: string[]): ParsedArgs => {
36
38
  const options: ParsedOption[] = [];
37
39
  const positionals: string[] = [];
38
40
  const unknown: string[] = [];
41
+ const incomplete: ParsedOption[] = [];
39
42
  for (let index = 0; index < args.length; index++) {
40
- const arity = arities.get(args[index]);
41
- if (arity === undefined) {
43
+ const getArity = arities.get(args[index]);
44
+ if (getArity === undefined) {
42
45
  (args[index].startsWith("-") ? unknown : positionals).push(args[index]);
43
46
  continue;
44
47
  }
45
- options.push({ name: args[index], values: args.slice(index + 1, index + 1 + arity) });
48
+ const arity = getArity(args.slice(index + 1));
49
+ const option = { name: args[index], values: args.slice(index + 1, index + 1 + arity) };
50
+ options.push(option);
51
+ if (option.values.length < arity) incomplete.push(option);
46
52
  index += arity;
47
53
  }
48
54
  return {
49
55
  options,
50
56
  positionals,
51
57
  unknown,
52
- incomplete: options.find((option) => option.values.length < (arities.get(option.name) ?? 0)),
58
+ incomplete: incomplete[0],
53
59
  has: (name) => options.some((option) => option.name === name),
54
60
  get: (name) => options.findLast((option) => option.name === name)?.values,
55
61
  };
package/src/cli.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import { enableCompileCache } from "node:module";
3
+ import { join } from "node:path";
3
4
 
4
- enableCompileCache?.();
5
+ // When tla-ts runs in place of java, it keeps its temporary files where Java would.
6
+ const JAVA_TMPDIR_PROPERTY = "-Djava.io.tmpdir=";
7
+ const javaTmpdir = process.argv
8
+ .find((arg) => arg.startsWith(JAVA_TMPDIR_PROPERTY))
9
+ ?.slice(JAVA_TMPDIR_PROPERTY.length);
10
+
11
+ enableCompileCache?.(javaTmpdir && join(javaTmpdir, "node-compile-cache"));
5
12
 
6
13
  void import("./run-cli").then((module) => module.runCli());
package/src/codegen.ts CHANGED
@@ -1,34 +1,36 @@
1
- import type { Expression } from "./ast";
2
- import { TlaEvaluationError } from "./errors";
1
+ import { getChildren, isLocallyResolved, type Expression } from "./ast";
3
2
  import {
4
- describeLocation,
5
3
  bindValue,
6
4
  expectBoolean,
7
5
  findGlobalName,
8
- getChildren,
9
- getEnumeratedElements,
10
6
  getConditionError,
11
- getConjunctError,
7
+ getJunctionError,
12
8
  getDomainOperator,
13
9
  getSubsetOperator,
14
10
  getEvaluator,
15
- getFieldError,
11
+ getFieldSelector,
12
+ getMissingArgumentDescriber,
13
+ getQuantifierElements,
16
14
  isClosedConstant,
17
- isLocallyResolved,
18
15
  isTlcInteger,
19
16
  isValueArgument,
20
17
  isWorthCaching,
21
18
  keepLazy,
22
19
  resolveGlobalOperator,
20
+ expectFunction,
21
+ toKeepLazyContext,
22
+ createPrimeOutsideActionError,
23
+ createUnassignedError,
24
+ getSortedFields,
25
+ hasDuplicateFields,
23
26
  type Context,
24
27
  type Environment,
25
28
  type Evaluator,
26
29
  } from "./evaluate";
27
30
  import {
28
31
  applyFunction,
29
- compareValues,
30
32
  createEnumeratedSet,
31
- createSortedFunction,
33
+ createSortedRecord,
32
34
  createTuple,
33
35
  createWrittenRecord,
34
36
  valuesEqual,
@@ -72,33 +74,32 @@ const createRuntime = () => ({
72
74
  if (typeof value !== "boolean") throw getConditionError(expression, value);
73
75
  return value;
74
76
  },
75
- conjunct: (value: Value, expression: Expression & { kind: "And" | "Or" }, index: number) => {
76
- if (typeof value !== "boolean") throw getConjunctError(expression, index, value);
77
+ junct: (value: Value, expression: Expression & { kind: "And" | "Or" }, index: number) => {
78
+ if (typeof value !== "boolean") throw getJunctionError(expression, index, value);
77
79
  return value;
78
80
  },
79
81
  apply: applyFunction,
82
+ expectFunction,
80
83
  tuple: createTuple,
81
84
  set: createEnumeratedSet,
82
- record: createSortedFunction,
85
+ record: createSortedRecord,
83
86
  writtenRecord: createWrittenRecord,
84
87
  equal: valuesEqual,
85
- enumerate: getEnumeratedElements,
88
+ enumerate: getQuantifierElements,
86
89
  unassigned: (name: string, expression: Expression): never => {
87
- throw new TlaEvaluationError(
88
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
89
- );
90
+ throw createUnassignedError(name, expression);
90
91
  },
91
92
  primeOutsideAction: (expression: Expression): never => {
92
- throw new TlaEvaluationError(
93
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
94
- );
93
+ throw createPrimeOutsideActionError(expression);
95
94
  },
96
95
  prime: (context: Context): Context => ({
97
96
  environment: context.environment,
98
97
  scope: context.scope,
99
98
  state: context.next,
100
99
  next: undefined,
100
+ isKeepLazy: context.isKeepLazy,
101
101
  }),
102
+ keepLazyContext: toKeepLazyContext,
102
103
  extend: (context: Context, names: string[], values: Value[]): Context => ({
103
104
  environment: context.environment,
104
105
  scope: names.reduce(
@@ -107,6 +108,7 @@ const createRuntime = () => ({
107
108
  ),
108
109
  state: context.state,
109
110
  next: context.next,
111
+ isKeepLazy: context.isKeepLazy,
110
112
  }),
111
113
  });
112
114
 
@@ -157,7 +159,7 @@ const generateQuantifier = (
157
159
  const lists = expression.bounds.map(() => createLocal(generator));
158
160
  const declarations = expression.bounds.map(
159
161
  (bound, index) =>
160
- `const ${lists[index]} = enumerate(${generateExpression(generator, bound.set, inner)}, ${addReference(generator, bound.set)});`,
162
+ `const ${lists[index]} = enumerate(${generateExpression(generator, bound.set, inner)}, ${addReference(generator, bound.set)}, ${JSON.stringify(generator.environment.runtime.version)});`,
161
163
  );
162
164
  const elements = expression.bounds.map((bound) => {
163
165
  const local = createLocal(generator);
@@ -165,9 +167,13 @@ const generateQuantifier = (
165
167
  return local;
166
168
  });
167
169
  const test = `if (bool(${generateExpression(generator, expression.body, inner)}, ${description}) !== ${isForall}) return ${!isForall};`;
170
+ // enumerate returns an array, or an interval, which has a low end, to step through.
168
171
  const loops = elements.reduce((code, element, index) => {
169
172
  const position = createLocal(generator);
170
- return `for (let ${position} = 0; ${position} < ${lists[index]}.length; ${position}++) { const ${element} = ${lists[index]}[${position}]; ${code} }`;
173
+ const low = createLocal(generator);
174
+ const size = createLocal(generator);
175
+ const list = lists[index];
176
+ return `for (let ${position} = 0, ${low} = ${list}.low, ${size} = ${low} === undefined ? ${list}.length : ${list}.high - ${low} + 1; ${position} < ${size}; ${position}++) { const ${element} = ${low} === undefined ? ${list}[${position}] : ${low} + ${position}; ${code} }`;
171
177
  }, test);
172
178
  const parameters = outer.map((local) => `, ${local}`).join("");
173
179
  generator.helpers.push(
@@ -215,6 +221,32 @@ const generateApply = (
215
221
  return generateDelegate(generator, expression, frame);
216
222
  };
217
223
 
224
+ // Whether an expression only reads values, so that it constructs no function whatever the control.
225
+ const isConstructionFree = (
226
+ expression: Expression,
227
+ frame: Frame,
228
+ environment: Environment,
229
+ ): boolean => {
230
+ switch (expression.kind) {
231
+ case "Identifier": {
232
+ if (frame.locals.has(expression.name)) return true;
233
+ if (isLocallyResolved(expression)) return false;
234
+ const global = findGlobalName(expression.name, environment);
235
+ return global?.kind === "Variable" || global?.kind === "Value";
236
+ }
237
+ case "Prime":
238
+ return isConstructionFree(expression.expression, frame, environment);
239
+ case "FieldAccess":
240
+ return isConstructionFree(expression.record, frame, environment);
241
+ case "FunctionApplication":
242
+ return [expression.function, ...expression.args].every((child) =>
243
+ isConstructionFree(child, frame, environment),
244
+ );
245
+ default:
246
+ return false;
247
+ }
248
+ };
249
+
218
250
  const generateExpression = (generator: Generator, expression: Expression, frame: Frame): string => {
219
251
  const generate = (node: Expression, nodeFrame = frame) =>
220
252
  generateExpression(generator, node, nodeFrame);
@@ -244,31 +276,39 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
244
276
  }
245
277
  case "Apply":
246
278
  return generateApply(generator, expression, frame);
247
- case "And": {
279
+ case "And":
280
+ case "Or": {
248
281
  const node = addReference(generator, expression);
249
- return `(${expression.operands.map((operand, index) => `conjunct(${generate(operand)}, ${node}, ${index})`).join(" && ")})`;
282
+ const operator = expression.kind === "And" ? " && " : " || ";
283
+ return `(${expression.operands.map((operand, index) => `junct(${generate(operand)}, ${node}, ${index})`).join(operator)})`;
250
284
  }
251
- case "Or":
252
- return `(${expression.operands.map((operand) => `bool(${generate(operand)}, "a disjunct")`).join(" || ")})`;
253
285
  case "If":
254
286
  return `(condition(${generate(expression.condition)}, ${addReference(generator, expression)}) ? ${generate(expression.consequent)} : ${generate(expression.alternative)})`;
255
287
  case "FunctionApplication": {
256
288
  keepLazy(expression.function);
257
- const functionCode = generate(expression.function);
289
+ // The interpreter evaluates a function that may construct one with EvalControl.KeepLazy.
290
+ const evaluatedFunction = isConstructionFree(
291
+ expression.function,
292
+ frame,
293
+ generator.environment,
294
+ )
295
+ ? generate(expression.function)
296
+ : generateDelegate(generator, expression.function, {
297
+ ...frame,
298
+ context: `keepLazyContext(${frame.context})`,
299
+ });
300
+ const functionCode = `expectFunction(${evaluatedFunction}, ${addReference(generator, expression)})`;
301
+ const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
302
+ const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
258
303
  if (expression.args.length === 1)
259
- return `apply(${functionCode}, ${generate(expression.args[0])})`;
260
- return `apply(${functionCode}, tuple([${expression.args.map((arg) => generate(arg)).join(", ")}]))`;
304
+ return `apply(${functionCode}, ${generate(expression.args[0])}${error})`;
305
+ return `apply(${functionCode}, tuple([${expression.args.map((arg) => generate(arg)).join(", ")}])${error})`;
261
306
  }
262
307
  case "FieldAccess":
263
- return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))})`;
308
+ return `${addReference(generator, getFieldSelector(expression))}(${generate(expression.record)})`;
264
309
  case "Record": {
265
- const fields = [...expression.fields].sort((left, right) =>
266
- compareValues(left.name, right.name),
267
- );
268
- const hasDuplicates = fields.some(
269
- (field, index) => index > 0 && fields[index - 1].name === field.name,
270
- );
271
- if (hasDuplicates) return generateDelegate(generator, expression, frame);
310
+ const fields = getSortedFields(expression.fields);
311
+ if (hasDuplicateFields(fields)) return generateDelegate(generator, expression, frame);
272
312
  const names = addReference(
273
313
  generator,
274
314
  fields.map((field) => field.name),