tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.868dd4c

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 (47) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +10 -0
  4. package/dist/index.d.ts +10 -0
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  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 +17 -16
  11. package/dist/run-cli.js +6 -5
  12. package/dist/transfer.cjs +27 -15
  13. package/dist/transfer.js +27 -15
  14. package/package.json +1 -1
  15. package/src/ast.ts +90 -0
  16. package/src/check.ts +259 -76
  17. package/src/cli-options.ts +11 -5
  18. package/src/codegen.ts +15 -22
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +88 -20
  21. package/src/errors.ts +18 -2
  22. package/src/evaluate.ts +117 -147
  23. package/src/exit-codes.ts +21 -43
  24. package/src/instances.ts +15 -14
  25. package/src/levels.ts +8 -8
  26. package/src/lexer.ts +5 -1
  27. package/src/live-check.ts +770 -0
  28. package/src/liveness.ts +54 -535
  29. package/src/parallel-worker.ts +7 -3
  30. package/src/parallel.ts +20 -4
  31. package/src/parser.ts +12 -1
  32. package/src/resolve.ts +16 -15
  33. package/src/run-cli.ts +58 -13
  34. package/src/runtime.ts +3 -1
  35. package/src/spec-description.ts +18 -6
  36. package/src/standard-modules.ts +2 -7
  37. package/src/state-graph.ts +157 -0
  38. package/src/subexpressions.ts +10 -10
  39. package/src/successors.ts +78 -14
  40. package/src/symmetry.ts +30 -8
  41. package/src/tableau.ts +51 -24
  42. package/src/tlc-ext.ts +29 -12
  43. package/src/tlc-module.ts +9 -28
  44. package/src/tlc-order.ts +25 -7
  45. package/src/tlc-random.ts +9 -6
  46. package/src/tlc-summary.ts +20 -5
  47. package/src/values.ts +128 -24
package/src/evaluate.ts CHANGED
@@ -1,14 +1,16 @@
1
- import type {
2
- Bound,
3
- BoundPattern,
4
- DecimalExpression,
5
- Definition,
6
- Expression,
7
- FunctionDefinition,
8
- NumberExpression,
9
- OperatorParam,
10
- RecordField,
11
- SourceRange,
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Bound,
5
+ type BoundPattern,
6
+ type DecimalExpression,
7
+ type Definition,
8
+ type Expression,
9
+ type FunctionDefinition,
10
+ type NumberExpression,
11
+ type OperatorParam,
12
+ type RecordField,
13
+ type SourceRange,
12
14
  } from "./ast";
13
15
  import { getKindString } from "./builtins";
14
16
  import { TlaEvaluationError } from "./errors";
@@ -28,23 +30,26 @@ import {
28
30
  createSortedSet,
29
31
  createWrittenRecord,
30
32
  createTuple,
33
+ describeMissingField,
31
34
  formatValue,
32
35
  getDomain,
33
36
  getFunctionDomain,
34
37
  getIntervalElements,
35
38
  getSequenceElements,
36
39
  getValueHash,
37
- isEnumerable,
38
40
  isFunctionValue,
39
41
  isOverriddenSet,
42
+ isRecordValue,
40
43
  isSameValue,
41
44
  markLambdaFunction,
45
+ isReducible,
42
46
  isSetValue,
43
47
  MAXIMUM_INTEGER,
44
48
  MINIMUM_INTEGER,
45
49
  toFiniteSet,
46
50
  updateFunction,
47
51
  valuesEqual,
52
+ type FunctionValue,
48
53
  type LazyFunctionSource,
49
54
  type LazyFunctionValue,
50
55
  type Value,
@@ -180,8 +185,17 @@ export const getConjunctError = (
180
185
  };
181
186
 
182
187
  export const getFieldError =
183
- (expression: Expression & { kind: "FieldAccess" }) => (record: Value) =>
184
- `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
188
+ (expression: Expression & { kind: "FieldAccess" }) => (record: FunctionValue) =>
189
+ isRecordValue(record)
190
+ ? `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`
191
+ : describeMissingField(record, expression.field);
192
+
193
+ export const getNonFunctionError = (expression: Expression) => (value: Value) =>
194
+ `A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(expression)}`;
195
+
196
+ export const getNonRecordError =
197
+ (expression: Expression & { kind: "FieldAccess" }) => (value: Value) =>
198
+ `Attempted to select field "${expression.field}" from a non-record value ${formatValue(value)}\n${describeRange(expression)}`;
185
199
 
186
200
  export const getDomainOperator = (expression: Expression) => (value: Value) => {
187
201
  if (!isFunctionValue(value)) {
@@ -262,96 +276,6 @@ export const expectBoolean = (value: Value, description: string) => {
262
276
  return value;
263
277
  };
264
278
 
265
- export const getChildren = (expression: Expression): Expression[] => {
266
- switch (expression.kind) {
267
- case "Number":
268
- case "Decimal":
269
- case "String":
270
- case "Identifier":
271
- case "OperatorReference":
272
- case "ExceptAt":
273
- return [];
274
- case "Apply":
275
- return expression.args;
276
- case "Lambda":
277
- return [expression.body];
278
- case "And":
279
- case "Or":
280
- return expression.operands;
281
- case "Prime":
282
- case "Unchanged":
283
- case "Enabled":
284
- case "Always":
285
- case "Eventually":
286
- return [expression.expression];
287
- case "If":
288
- return [expression.condition, expression.consequent, expression.alternative];
289
- case "Case":
290
- return [
291
- ...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
292
- ...(expression.other ? [expression.other] : []),
293
- ];
294
- case "Let":
295
- return [
296
- ...expression.definitions.flatMap((definition) =>
297
- definition.kind === "Function"
298
- ? [...definition.bounds.map((bound) => bound.set), definition.body]
299
- : [definition.body],
300
- ),
301
- expression.body,
302
- ];
303
- case "Forall":
304
- case "Exists":
305
- return [...expression.bounds.map((bound) => bound.set), expression.body];
306
- case "UnboundedQuantifier":
307
- return [expression.body];
308
- case "Choose":
309
- return [...(expression.set ? [expression.set] : []), expression.body];
310
- case "SetEnumeration":
311
- case "Tuple":
312
- return expression.elements;
313
- case "SetFilter":
314
- return [expression.bound.set, expression.predicate];
315
- case "SetMap":
316
- return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
317
- case "FunctionConstruction":
318
- return [...expression.bounds.map((bound) => bound.set), expression.body];
319
- case "FunctionSet":
320
- return [expression.domain, expression.range];
321
- case "Record":
322
- case "RecordSet":
323
- return expression.fields.map((field) => field.value);
324
- case "Except":
325
- return [
326
- expression.function,
327
- ...expression.updates.flatMap((update) => [
328
- ...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
329
- update.value,
330
- ]),
331
- ];
332
- case "FunctionApplication":
333
- return [expression.function, ...expression.args];
334
- case "FieldAccess":
335
- return [expression.record];
336
- case "Product":
337
- return expression.factors;
338
- case "BoxAction":
339
- case "AngleAction":
340
- return [expression.action, expression.subscript];
341
- case "WeakFairness":
342
- case "StrongFairness":
343
- return [expression.subscript, expression.action];
344
- case "Label":
345
- return [expression.body];
346
- case "Subexpression":
347
- return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
348
- }
349
- };
350
-
351
- export const isLocallyResolved = (expression: Expression) =>
352
- (expression.kind === "Identifier" || expression.kind === "Apply") &&
353
- expression.resolution?.kind === "Local";
354
-
355
279
  export const getDefinition = (name: string, environment: Environment) =>
356
280
  environment.definitions.get(environment.substitutions.get(name) ?? name);
357
281
 
@@ -859,11 +783,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
859
783
  const global = findGlobalName(name, context.environment);
860
784
  if (global?.kind === "Variable") {
861
785
  const value = context.state?.[global.index];
862
- if (value === undefined) {
863
- throw new TlaEvaluationError(
864
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
865
- );
866
- }
786
+ if (value === undefined) throw createUnassignedError(name, expression);
867
787
  return value;
868
788
  }
869
789
  if (global?.kind === "Value") return global.value;
@@ -889,14 +809,29 @@ const IMPURE_BUILTINS = new Set([
889
809
  export const isImpureBuiltin = (name: string, environment: Environment) =>
890
810
  IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
891
811
 
892
- const impureDefinitions = new WeakMap<Definition, boolean>();
812
+ interface Impurity {
813
+ builtins: Set<string>;
814
+ definitions: WeakMap<Definition, boolean>;
815
+ }
816
+
817
+ const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
818
+
819
+ // An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
820
+ const REPEATED_EFFECT: Impurity = {
821
+ builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
822
+ definitions: new WeakMap(),
823
+ };
893
824
 
894
825
  const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
895
826
  definition.kind === "Operator" &&
896
827
  definition.params.length === 0 &&
897
828
  isConstantLevel(definition, environment);
898
829
 
899
- const callsImpureBuiltin = (expression: Expression, environment: Environment): boolean => {
830
+ const callsImpureBuiltin = (
831
+ expression: Expression,
832
+ environment: Environment,
833
+ impurity: Impurity,
834
+ ): boolean => {
900
835
  const name =
901
836
  expression.kind === "Identifier"
902
837
  ? expression.name
@@ -905,29 +840,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
905
840
  : undefined;
906
841
  if (name !== undefined && !isLocallyResolved(expression)) {
907
842
  const definition = getDefinition(name, environment);
908
- if (!definition && isImpureBuiltin(name, environment)) return true;
843
+ const isImpureCall =
844
+ impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
845
+ if (!definition && isImpureCall) return true;
909
846
  if (
910
847
  definition &&
911
848
  !isEvaluatedOnce(definition, environment) &&
912
- isImpureDefinition(definition, environment)
849
+ isImpureDefinition(definition, environment, impurity)
913
850
  ) {
914
851
  return true;
915
852
  }
916
853
  }
917
- return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
854
+ return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
918
855
  };
919
856
 
920
857
  // TLC evaluates a constant definition without parameters once, so only other definitions pass
921
858
  // on the side effects and random choices of the builtins they call.
922
- const isImpureDefinition = (definition: Definition, environment: Environment): boolean => {
923
- const cached = impureDefinitions.get(definition);
859
+ const isImpureDefinition = (
860
+ definition: Definition,
861
+ environment: Environment,
862
+ impurity = ANY_EFFECT,
863
+ ): boolean => {
864
+ const cached = impurity.definitions.get(definition);
924
865
  if (cached !== undefined) return cached;
925
- impureDefinitions.set(definition, false);
866
+ impurity.definitions.set(definition, false);
926
867
  const isImpure =
927
- callsImpureBuiltin(definition.body, environment) ||
868
+ callsImpureBuiltin(definition.body, environment, impurity) ||
928
869
  (definition.kind === "Function" &&
929
- definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
930
- impureDefinitions.set(definition, isImpure);
870
+ definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
871
+ impurity.definitions.set(definition, isImpure);
931
872
  return isImpure;
932
873
  };
933
874
 
@@ -941,6 +882,12 @@ export const isImpure = (environment: Environment) =>
941
882
  isImpureDefinition(definition, environment),
942
883
  );
943
884
 
885
+ // Whether evaluating a definition again can show, such as by printing twice.
886
+ export const hasRepeatedEffects = (environment: Environment) =>
887
+ [...environment.definitions.values()].some((definition) =>
888
+ isImpureDefinition(definition, environment, REPEATED_EFFECT),
889
+ );
890
+
944
891
  const closedConstants = new WeakMap<Expression, boolean>();
945
892
 
946
893
  export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
@@ -1092,7 +1039,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
1092
1039
 
1093
1040
  const entryEvaluators = new WeakMap<Expression, Evaluator>();
1094
1041
 
1095
- const getEntryEvaluator = (expression: Expression, environment: Environment) => {
1042
+ export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
1096
1043
  const cached = entryEvaluators.get(expression);
1097
1044
  if (cached) return cached;
1098
1045
  const evaluator =
@@ -1116,11 +1063,7 @@ const compileIdentifier = (
1116
1063
  const index = global.index;
1117
1064
  return (context) => {
1118
1065
  const value = context.state?.[index];
1119
- if (value === undefined) {
1120
- throw new TlaEvaluationError(
1121
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
1122
- );
1123
- }
1066
+ if (value === undefined) throw createUnassignedError(name, expression);
1124
1067
  return value;
1125
1068
  };
1126
1069
  }
@@ -1489,7 +1432,13 @@ const compileFunctionConstruction = (
1489
1432
  const set = getEvaluator(bound.set, environment);
1490
1433
  return (context) => {
1491
1434
  const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
1492
- if (expression.isRecursive || isKeptLazy || !isEnumerable(domain)) {
1435
+ // TLC builds the function only when it reduces every domain, and evaluates the body lazily
1436
+ // otherwise.
1437
+ const domains =
1438
+ typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
1439
+ ? domain.factors
1440
+ : [domain];
1441
+ if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
1493
1442
  const functionValue = createLazyFunction(
1494
1443
  domain,
1495
1444
  (argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
@@ -1583,10 +1532,7 @@ const compileSetComprehension = (
1583
1532
  predicate(withScope(context, bindPattern(context.scope, pattern, element))),
1584
1533
  "a set filter predicate",
1585
1534
  );
1586
- const isReducible =
1587
- typeof base === "object" &&
1588
- (base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
1589
- if (!isReducible) {
1535
+ if (!isReducible(base)) {
1590
1536
  if (isSetValue(base)) return { kind: "FilteredSet", base, test };
1591
1537
  throw new TlaEvaluationError(
1592
1538
  `TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
@@ -1632,11 +1578,26 @@ const compileSetComprehension = (
1632
1578
  };
1633
1579
  };
1634
1580
 
1635
- const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1636
- const fields = [...recordFields].sort((left, right) => compareValues(left.name, right.name));
1637
- const hasDuplicates = fields.some(
1638
- (field, index) => index > 0 && fields[index - 1].name === field.name,
1581
+ // TLC orders a record's fields by name.
1582
+ export const getSortedFields = (fields: RecordField[]) =>
1583
+ [...fields].sort((left, right) => compareValues(left.name, right.name));
1584
+
1585
+ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
1586
+ sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
1587
+
1588
+ export const createUnassignedError = (name: string, expression: Expression) =>
1589
+ new TlaEvaluationError(
1590
+ `In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
1591
+ );
1592
+
1593
+ export const createPrimeOutsideActionError = (expression: Expression) =>
1594
+ new TlaEvaluationError(
1595
+ `A primed expression appears outside an action at ${describeLocation(expression)}`,
1639
1596
  );
1597
+
1598
+ const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1599
+ const fields = getSortedFields(recordFields);
1600
+ const hasDuplicates = hasDuplicateFields(fields);
1640
1601
  const values = fields.map((field) => getEvaluator(field.value, environment));
1641
1602
  if (hasDuplicates) {
1642
1603
  return (context) =>
@@ -1714,11 +1675,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1714
1675
  case "Prime": {
1715
1676
  const inner = child(expression.expression);
1716
1677
  return (context) => {
1717
- if (!context.next) {
1718
- throw new TlaEvaluationError(
1719
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
1720
- );
1721
- }
1678
+ if (!context.next) throw createPrimeOutsideActionError(expression);
1722
1679
  return inner({
1723
1680
  environment: context.environment,
1724
1681
  scope: context.scope,
@@ -1757,7 +1714,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1757
1714
  if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
1758
1715
  }
1759
1716
  if (other) return other(context);
1760
- throw new TlaEvaluationError(`No CASE guard is true at ${describeLocation(expression)}`);
1717
+ throw new TlaEvaluationError(
1718
+ `Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
1719
+ );
1761
1720
  };
1762
1721
  }
1763
1722
  case "Let": {
@@ -1796,9 +1755,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1796
1755
  case "Record":
1797
1756
  return compileRecord(expression.fields, environment);
1798
1757
  case "RecordSet": {
1799
- const fields = [...expression.fields].sort((left, right) =>
1800
- compareValues(left.name, right.name),
1801
- );
1758
+ const fields = getSortedFields(expression.fields);
1802
1759
  const names = fields.map((field) => field.name);
1803
1760
  const sets = fields.map((field) => child(field.value));
1804
1761
  return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
@@ -1819,18 +1776,31 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1819
1776
  keepLazy(expression.function);
1820
1777
  const evaluateFunction = child(expression.function);
1821
1778
  const args = expression.args.map(child);
1779
+ const describeNonFunction = getNonFunctionError(expression);
1822
1780
  if (args.length === 1) {
1823
1781
  const argument = args[0];
1824
- return (context) => applyFunction(evaluateFunction(context), argument(context));
1782
+ return (context) =>
1783
+ applyFunction(
1784
+ evaluateFunction(context),
1785
+ argument(context),
1786
+ undefined,
1787
+ describeNonFunction,
1788
+ );
1825
1789
  }
1826
1790
  return (context) =>
1827
- applyFunction(evaluateFunction(context), createTuple(args.map((arg) => arg(context))));
1791
+ applyFunction(
1792
+ evaluateFunction(context),
1793
+ createTuple(args.map((arg) => arg(context))),
1794
+ undefined,
1795
+ describeNonFunction,
1796
+ );
1828
1797
  }
1829
1798
  case "FieldAccess": {
1830
1799
  const record = child(expression.record);
1831
1800
  const field = expression.field;
1832
1801
  const describeMissing = getFieldError(expression);
1833
- return (context) => applyFunction(record(context), field, describeMissing);
1802
+ const describeNonRecord = getNonRecordError(expression);
1803
+ return (context) => applyFunction(record(context), field, describeMissing, describeNonRecord);
1834
1804
  }
1835
1805
  case "Product": {
1836
1806
  const factors = expression.factors.map(child);
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,30 @@ 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
+ addInitialState: (index, state) =>
45
+ withErrorExitCode(() => checker.addInitialState(index, state)),
46
+ addState: (index, state, successors) =>
47
+ withErrorExitCode(() => checker.addState(index, state, successors)),
48
+ findViolation: () => withErrorExitCode(() => checker.findViolation()),
49
+ getViolatedProperties: (path, cycleStart) =>
50
+ withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
51
+ }
52
+ );
53
+ };
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>();
@@ -218,7 +219,7 @@ interface Instantiation {
218
219
  variables: Set<string>;
219
220
  getDefinition: (key: string) => Definition | undefined;
220
221
  isParameterized: (key: string) => boolean;
221
- addDefinition: (name: string, create: (key: string) => Definition) => string;
222
+ addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
222
223
  createName: (name: string) => string;
223
224
  }
224
225
 
@@ -639,7 +640,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
639
640
  const instantiation = rewrite.instantiation;
640
641
  const definition = instantiation.getDefinition(key);
641
642
  if (!definition) return key;
642
- return instantiation.addDefinition(definition.name, (copyKey) => {
643
+ return instantiation.addDefinition(key, definition.name, (copyKey) => {
643
644
  rewrite.copies.set(key, copyKey);
644
645
  const rename = createRenamer(rewrite);
645
646
  const mapped = renameParams(
@@ -678,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
678
679
  if (copy !== undefined) return copy;
679
680
  const definition = instantiation.getDefinition(key);
680
681
  if (!definition) return key;
681
- return instantiation.addDefinition(definition.name, () =>
682
+ return instantiation.addDefinition(key, definition.name, () =>
682
683
  withInstanceParams(definition, instantiation.params, definition.location),
683
684
  );
684
685
  },
package/src/levels.ts CHANGED
@@ -1,13 +1,13 @@
1
- import type {
2
- AssumeProve,
3
- Definition,
4
- Expression,
5
- Module,
6
- OperatorParam,
7
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type AssumeProve,
4
+ type Definition,
5
+ type Expression,
6
+ type Module,
7
+ type OperatorParam,
8
+ type SourceLocation,
8
9
  } from "./ast";
9
10
  import { TlaSyntaxError } from "./errors";
10
- import { getChildren } from "./evaluate";
11
11
  import { mapChildren } from "./instances";
12
12
  import { getBoundNames, getPatternNames } from "./resolve";
13
13
  import { STANDARD_LEVELS } from "./standard-levels";
package/src/lexer.ts CHANGED
@@ -345,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
345
345
  if (!headerMatch)
346
346
  throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
347
347
  const tokens: Token[] = [];
348
+ // Each name is one string object, so looking a name up in a scope compares pointers.
349
+ const names = new Map<string, string>();
348
350
  const prefixLines = source.slice(0, headerMatch.index).split("\n");
349
351
  const cursor = {
350
352
  index: headerMatch.index,
@@ -481,7 +483,9 @@ export const tokenize = (source: string): Token[] => {
481
483
  pushToken("Keyword", word.slice(0, 3), 3);
482
484
  continue;
483
485
  }
484
- pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", word, word.length);
486
+ const name = names.get(word) ?? word;
487
+ names.set(name, name);
488
+ pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
485
489
  continue;
486
490
  }
487
491
  const stepLength = matchStep(source, cursor.index);