tla-ts 0.0.2 → 0.0.3-dev.14337b3

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 (65) hide show
  1. package/README.md +78 -12
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +85 -15
  4. package/dist/index.d.ts +85 -15
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +22 -2
  11. package/dist/run-cli.js +22 -2
  12. package/dist/transfer.cjs +37 -16
  13. package/dist/transfer.js +37 -16
  14. package/package.json +3 -1
  15. package/src/ast.ts +137 -4
  16. package/src/check.ts +378 -100
  17. package/src/cli-options.ts +64 -0
  18. package/src/codegen.ts +15 -22
  19. package/src/config.ts +224 -170
  20. package/src/dependencies.ts +2 -2
  21. package/src/environment.ts +116 -28
  22. package/src/errors.ts +101 -3
  23. package/src/evaluate.ts +129 -153
  24. package/src/exit-codes.ts +39 -42
  25. package/src/fingerprints.ts +46 -11
  26. package/src/instances.ts +16 -15
  27. package/src/levels.ts +62 -34
  28. package/src/lexer.ts +11 -2
  29. package/src/live-check.ts +772 -0
  30. package/src/liveness.ts +116 -543
  31. package/src/obligation-action.ts +384 -0
  32. package/src/obligation-anon.ts +575 -0
  33. package/src/obligation-deref.ts +637 -0
  34. package/src/obligation-expression.ts +1060 -0
  35. package/src/obligation-format.ts +406 -0
  36. package/src/obligation-generate.ts +1074 -0
  37. package/src/obligation-levels.ts +329 -0
  38. package/src/obligation-module.ts +1042 -0
  39. package/src/obligation-printer.ts +889 -0
  40. package/src/obligation-proof.ts +76 -0
  41. package/src/obligation-source.ts +1084 -0
  42. package/src/obligation-tuples.ts +184 -0
  43. package/src/obligations.ts +316 -0
  44. package/src/parallel-worker.ts +168 -212
  45. package/src/parallel.ts +78 -37
  46. package/src/parser.ts +315 -60
  47. package/src/resolve.ts +122 -44
  48. package/src/run-cli.ts +376 -44
  49. package/src/runtime.ts +3 -1
  50. package/src/spec-description.ts +19 -8
  51. package/src/standard-levels.ts +3 -3
  52. package/src/standard-modules.ts +2 -7
  53. package/src/state-graph.ts +157 -0
  54. package/src/subexpressions.ts +10 -10
  55. package/src/successors.ts +99 -25
  56. package/src/symmetry.ts +30 -8
  57. package/src/tableau.ts +51 -24
  58. package/src/tlc-ext.ts +29 -12
  59. package/src/tlc-module.ts +14 -33
  60. package/src/tlc-modules.ts +4 -0
  61. package/src/tlc-order.ts +25 -7
  62. package/src/tlc-random.ts +9 -6
  63. package/src/tlc-summary.ts +20 -5
  64. package/src/transfer.ts +22 -1
  65. package/src/values.ts +179 -42
package/src/resolve.ts CHANGED
@@ -1,20 +1,28 @@
1
- import type {
2
- AssumeProve,
3
- Bound,
4
- BoundPattern,
5
- Definition,
6
- Expression,
7
- Instance,
8
- Module,
9
- NewSymbol,
10
- OperatorParam,
11
- Proof,
12
- ProofStep,
13
- SourceLocation,
14
- Theorem,
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type AssumeProve,
5
+ type Bound,
6
+ type BoundPattern,
7
+ type Definition,
8
+ type Expression,
9
+ type Instance,
10
+ type Module,
11
+ type NewSymbol,
12
+ type OperatorParam,
13
+ type Proof,
14
+ type ProofStep,
15
+ type SourceLocation,
16
+ type SourceRange,
17
+ type Theorem,
15
18
  } from "./ast";
16
- import { TlaSyntaxError } from "./errors";
17
- import { getChildren, isLocallyResolved } from "./evaluate";
19
+ import {
20
+ createSanyError,
21
+ createSemanticError,
22
+ formatRange,
23
+ TlaSyntaxError,
24
+ type SanyError,
25
+ } from "./errors";
18
26
 
19
27
  export interface ModuleSymbol {
20
28
  key: string;
@@ -102,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
102
110
  : unit.imported.get(name);
103
111
  };
104
112
 
113
+ // SANY reports a name at the range of its text.
114
+ const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
115
+ module: location.range?.module ?? "",
116
+ start: location,
117
+ end: { line: location.line, column: location.column + name.length - 1 },
118
+ });
119
+
120
+ // SANY reports an operator symbol applied to operands as one it cannot resolve.
121
+ const getFixity = (expression: Expression) => {
122
+ if (expression.kind !== "Apply") return undefined;
123
+ if (expression.args.length === 2) return "infix";
124
+ if (expression.args.length !== 1) return undefined;
125
+ return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
126
+ };
127
+
105
128
  // SANY names an undefined operator symbol such as + differently from an undefined identifier.
106
129
  const isOperatorSymbol = (name: string) =>
107
130
  name[0].toLowerCase() === name[0].toUpperCase() &&
@@ -134,7 +157,7 @@ const getSymbolArity = (
134
157
  }
135
158
  // A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
136
159
  // arguments.
137
- const prefix = name.slice(0, name.indexOf("!"));
160
+ const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
138
161
  const isSelected =
139
162
  prefix !== "" &&
140
163
  (lookupLocal(scope, prefix) !== undefined ||
@@ -143,11 +166,23 @@ const getSymbolArity = (
143
166
  if (isSelected) {
144
167
  return expression.kind === "Apply" ? expression.args.length : 0;
145
168
  }
146
- const position = `at line ${location.line}, column ${location.column}`;
147
- throw new TlaSyntaxError(
148
- isOperatorSymbol(name)
149
- ? `Could not find declaration or definition of symbol '${name}' ${position}`
150
- : `Unknown operator: \`${name}' ${position}`,
169
+ const range = getNameRange(location, name === "-." ? "-" : name);
170
+ if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
171
+ const missing = {
172
+ location: formatRange(range),
173
+ message: `Could not find declaration or definition of symbol '${name}'.`,
174
+ };
175
+ const fixity = getFixity(expression);
176
+ throw createSanyError(
177
+ fixity && location.range
178
+ ? [
179
+ missing,
180
+ {
181
+ location: formatRange(location.range),
182
+ message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
183
+ },
184
+ ]
185
+ : [missing],
151
186
  );
152
187
  };
153
188
 
@@ -234,13 +269,13 @@ const resolveApply = (
234
269
  scope: LocalName | undefined,
235
270
  unit: Unit,
236
271
  ) => {
237
- const callee = lookupLocal(scope, expression.operator)
238
- ? undefined
239
- : getVisibleDefinition(expression.operator, unit);
240
- const arity = getSymbolArity(expression, expression.operator, scope, unit);
272
+ const operator = expression.operator;
273
+ const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
274
+ const arity = getSymbolArity(expression, operator, scope, unit);
241
275
  if (arity !== expression.args.length) {
242
- throw new TlaSyntaxError(
243
- `The operator ${expression.operator} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
276
+ throw createSemanticError(
277
+ `The operator ${operator} requires ${arity} arguments.`,
278
+ expression.argumentsRange ?? getNameRange(expression.location, operator),
244
279
  );
245
280
  }
246
281
  expression.args.forEach((arg, index) => {
@@ -274,10 +309,12 @@ const resolveExpression = (
274
309
  case "ExceptAt":
275
310
  return;
276
311
  case "Identifier": {
277
- const arity = getSymbolArity(expression, expression.name, scope, unit);
312
+ const name = expression.name;
313
+ const arity = getSymbolArity(expression, name, scope, unit);
278
314
  if (arity > 0) {
279
- throw new TlaSyntaxError(
280
- `The operator ${expression.name} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
315
+ throw createSemanticError(
316
+ `The operator ${name} requires ${arity} arguments.`,
317
+ getNameRange(expression.location, name),
281
318
  );
282
319
  }
283
320
  return;
@@ -346,14 +383,21 @@ const resolveExpression = (
346
383
  resolveAll([expression.domain, expression.range]);
347
384
  return;
348
385
  case "Record":
349
- case "RecordSet":
350
- if (new Set(expression.fields.map((field) => field.name)).size < expression.fields.length) {
351
- throw new TlaSyntaxError(
352
- `Non-unique fields in constructor at line ${expression.location.line}, column ${expression.location.column}`,
386
+ case "RecordSet": {
387
+ const repeated = expression.fields.filter((field, index) =>
388
+ expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
389
+ );
390
+ if (repeated.length > 0) {
391
+ throw createSanyError(
392
+ repeated.map((field) => ({
393
+ location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
394
+ message: "Non-unique fields in constructor.",
395
+ })),
353
396
  );
354
397
  }
355
398
  resolveAll(expression.fields.map((field) => field.value));
356
399
  return;
400
+ }
357
401
  case "Except":
358
402
  resolveAll([
359
403
  expression.function,
@@ -768,6 +812,29 @@ export const resolveSubstitutions = (
768
812
  );
769
813
  };
770
814
 
815
+ // SANY reports the errors of every unit it checks, in the order of the units in the module.
816
+ export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
817
+ const errors: SanyError[] = [];
818
+ units
819
+ .toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
820
+ .forEach((unit) => {
821
+ try {
822
+ unit.check();
823
+ } catch (error) {
824
+ if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
825
+ error.sany.errors
826
+ .filter((item) =>
827
+ errors.every(
828
+ (existing) =>
829
+ existing.location !== item.location || existing.message !== item.message,
830
+ ),
831
+ )
832
+ .forEach((item) => errors.push(item));
833
+ }
834
+ });
835
+ if (errors.length > 0) throw createSanyError(errors);
836
+ };
837
+
771
838
  export const resolveModule = (module: Module, unit: ModuleUnit) => {
772
839
  const symbols = createSymbols(module, unit.imported);
773
840
  const facts = new Set(
@@ -781,13 +848,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
781
848
  self,
782
849
  facts,
783
850
  });
784
- module.definitions.forEach((definition) =>
785
- resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
786
- );
787
- module.assumptions.forEach((assumption) =>
788
- resolveExpression(assumption.expression, undefined, createUnit(assumption.location, undefined)),
789
- );
790
- module.theorems.forEach((theorem) =>
791
- resolveTheorem(theorem, createUnit(theorem.location, undefined)),
792
- );
851
+ collectSemanticErrors([
852
+ ...module.definitions.map((definition) => ({
853
+ location: definition.location,
854
+ check: () =>
855
+ resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
856
+ })),
857
+ ...module.assumptions.map((assumption) => ({
858
+ location: assumption.location,
859
+ check: () =>
860
+ resolveExpression(
861
+ assumption.expression,
862
+ undefined,
863
+ createUnit(assumption.location, undefined),
864
+ ),
865
+ })),
866
+ ...module.theorems.map((theorem) => ({
867
+ location: theorem.location,
868
+ check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
869
+ })),
870
+ ]);
793
871
  };