tla-ts 0.0.3-dev.e946acb → 0.0.3-dev.e9d8347

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 (48) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +9 -0
  4. package/dist/index.d.ts +9 -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 +94 -24
  21. package/src/errors.ts +18 -2
  22. package/src/evaluate.ts +122 -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 +79 -15
  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 +14 -33
  44. package/src/tlc-modules.ts +4 -0
  45. package/src/tlc-order.ts +25 -7
  46. package/src/tlc-random.ts +9 -6
  47. package/src/tlc-summary.ts +20 -5
  48. package/src/values.ts +121 -20
package/src/codegen.ts CHANGED
@@ -1,11 +1,8 @@
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
6
  getEnumeratedElements,
10
7
  getConditionError,
11
8
  getConjunctError,
@@ -13,20 +10,24 @@ import {
13
10
  getSubsetOperator,
14
11
  getEvaluator,
15
12
  getFieldError,
13
+ getNonFunctionError,
14
+ getNonRecordError,
16
15
  isClosedConstant,
17
- isLocallyResolved,
18
16
  isTlcInteger,
19
17
  isValueArgument,
20
18
  isWorthCaching,
21
19
  keepLazy,
22
20
  resolveGlobalOperator,
21
+ createPrimeOutsideActionError,
22
+ createUnassignedError,
23
+ getSortedFields,
24
+ hasDuplicateFields,
23
25
  type Context,
24
26
  type Environment,
25
27
  type Evaluator,
26
28
  } from "./evaluate";
27
29
  import {
28
30
  applyFunction,
29
- compareValues,
30
31
  createEnumeratedSet,
31
32
  createSortedFunction,
32
33
  createTuple,
@@ -84,14 +85,10 @@ const createRuntime = () => ({
84
85
  equal: valuesEqual,
85
86
  enumerate: getEnumeratedElements,
86
87
  unassigned: (name: string, expression: Expression): never => {
87
- throw new TlaEvaluationError(
88
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
89
- );
88
+ throw createUnassignedError(name, expression);
90
89
  },
91
90
  primeOutsideAction: (expression: Expression): never => {
92
- throw new TlaEvaluationError(
93
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
94
- );
91
+ throw createPrimeOutsideActionError(expression);
95
92
  },
96
93
  prime: (context: Context): Context => ({
97
94
  environment: context.environment,
@@ -255,20 +252,16 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
255
252
  case "FunctionApplication": {
256
253
  keepLazy(expression.function);
257
254
  const functionCode = generate(expression.function);
255
+ const describeNonFunction = addReference(generator, getNonFunctionError(expression));
258
256
  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(", ")}]))`;
257
+ return `apply(${functionCode}, ${generate(expression.args[0])}, undefined, ${describeNonFunction})`;
258
+ return `apply(${functionCode}, tuple([${expression.args.map((arg) => generate(arg)).join(", ")}]), undefined, ${describeNonFunction})`;
261
259
  }
262
260
  case "FieldAccess":
263
- return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))})`;
261
+ return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))}, ${addReference(generator, getNonRecordError(expression))})`;
264
262
  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);
263
+ const fields = getSortedFields(expression.fields);
264
+ if (hasDuplicateFields(fields)) return generateDelegate(generator, expression, frame);
272
265
  const names = addReference(
273
266
  generator,
274
267
  fields.map((field) => field.name),
@@ -1,5 +1,5 @@
1
- import type { Definition, Expression } from "./ast";
2
- import { getChildren, isImpureBuiltin, type Environment } from "./evaluate";
1
+ import { getChildren, type Definition, type Expression } from "./ast";
2
+ import { isImpureBuiltin, type Environment } from "./evaluate";
3
3
 
4
4
  const STATE_INDEPENDENT_KINDS = new Set([
5
5
  "Prime",
@@ -1,23 +1,24 @@
1
- import type {
2
- Assumption,
3
- Declaration,
4
- Definition,
5
- Expression,
6
- Instance,
7
- Module,
8
- OperatorParam,
9
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type Assumption,
4
+ type Declaration,
5
+ type Definition,
6
+ type Expression,
7
+ type Instance,
8
+ type Module,
9
+ type OperatorParam,
10
+ type SourceLocation,
10
11
  } from "./ast";
11
12
  import { BUNDLED_MODULES } from "./bundled-modules";
12
13
  import type { ConfigValue, ModelConfig } from "./config";
13
14
  import type { FileSystem } from "./json";
14
- import { TlaConfigError, TlaEvaluationError, TlaSyntaxError } from "./errors";
15
+ import { TlaConfigError, TlaEvaluationError, TlaModuleError, TlaSyntaxError } from "./errors";
15
16
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
16
17
  import {
17
18
  createBigNumberError,
18
19
  createRealNumberError,
19
- getChildren,
20
- isConstantLevel,
20
+ evaluate,
21
+ getDefinition,
21
22
  isTlcInteger,
22
23
  type Builtin,
23
24
  type Environment,
@@ -44,8 +45,9 @@ import {
44
45
  import { createRuntime } from "./runtime";
45
46
  import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
46
47
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
47
- import { checkModuleLevels } from "./levels";
48
+ import { checkModuleLevels, createLevelAnalyzer } from "./levels";
48
49
  import { checkInstances, type InstanceCheck } from "./substitution-levels";
50
+ import { getTlcDeclarationOrder } from "./tlc-order";
49
51
  import {
50
52
  checkInteger,
51
53
  createModelValue,
@@ -212,6 +214,8 @@ export const createEnvironment = (
212
214
  const root = parseModule(options.source);
213
215
  const definitions = new Map<string, Definition>();
214
216
  const parameterized = new Map<string, boolean>();
217
+ // The definition each instance copy came from, and the instance parameters the copy adds.
218
+ const copySources = new Map<string, { source: string; prefix: number }>();
215
219
  const builtins = new Map<string, Builtin>(
216
220
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
217
221
  );
@@ -379,10 +383,11 @@ export const createEnvironment = (
379
383
  variables: new Set(linked.variables),
380
384
  getDefinition: (key) => definitions.get(key),
381
385
  isParameterized: (key) => parameterized.get(key) === true,
382
- addDefinition: (name, create) => {
386
+ addDefinition: (source, name, create) => {
383
387
  const key = createName(`${name}@${module.name}`);
384
388
  definitions.set(key, create(key));
385
389
  parameterized.set(key, !isParameterFree);
390
+ copySources.set(key, { source, prefix: instance.params.length });
386
391
  return key;
387
392
  },
388
393
  createName,
@@ -544,7 +549,7 @@ export const createEnvironment = (
544
549
  variables: new Set(linked.variables),
545
550
  getDefinition: (key) => definitions.get(key),
546
551
  isParameterized: (key) => parameterized.get(key) === true,
547
- addDefinition: (name, create) => {
552
+ addDefinition: (_source, name, create) => {
548
553
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
549
554
  const definition = create(key);
550
555
  copies.push(
@@ -872,11 +877,51 @@ export const createEnvironment = (
872
877
  );
873
878
  }
874
879
  });
875
- declaredConstants.forEach((name) => {
876
- if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
877
- throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
878
- }
880
+ // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
881
+ // the module shares.
882
+ const scopedKeys = new Set(
883
+ [...config.constants, ...config.substitutions]
884
+ .filter((item) => item.module !== undefined)
885
+ .flatMap(getScopedKeys),
886
+ );
887
+ const getOverride = (
888
+ key: string,
889
+ prefix: number,
890
+ ): { key: string; prefix: number } | undefined => {
891
+ if (scopedKeys.has(key)) return { key, prefix };
892
+ const copy = copySources.get(key);
893
+ return copy && getOverride(copy.source, prefix + copy.prefix);
894
+ };
895
+ copySources.forEach((copy, key) => {
896
+ const override = getOverride(copy.source, copy.prefix);
897
+ const definition = definitions.get(key);
898
+ if (!override || !definition) return;
899
+ const params = definition.kind === "Operator" ? definition.params : [];
900
+ const alias = createLocalAlias(
901
+ key,
902
+ override.key,
903
+ params,
904
+ params.slice(override.prefix),
905
+ definition.location,
906
+ );
907
+ definitions.set(key, alias);
908
+ linkedRoot.visible.forEach((symbol, name) => {
909
+ if (symbol.key === key && name !== key) definitions.set(name, alias);
910
+ });
879
911
  });
912
+ const unassigned = declaredConstants.filter(
913
+ (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
914
+ );
915
+ if (unassigned.length > 0) {
916
+ // TLC reports the first constant it finds unassigned in SANY's order of declarations.
917
+ const name =
918
+ getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
919
+ unassigned.includes(constant),
920
+ ) ?? unassigned[0];
921
+ throw new TlaConfigError(
922
+ `The constant parameter ${name} is not assigned a value by the configuration file.`,
923
+ );
924
+ }
880
925
 
881
926
  const environment: Environment = {
882
927
  runtime,
@@ -907,18 +952,43 @@ export const createEnvironment = (
907
952
  getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
908
953
  };
909
954
  });
955
+ // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
956
+ // computes is above constant. The bound counts the operators that TLC's Java code overrides with
957
+ // a minimum level as state-level, so the analyzer takes them for variables.
958
+ const analyzer = createLevelAnalyzer({
959
+ constants: new Set(constants.keys()),
960
+ variables: new Set([
961
+ ...variables,
962
+ ...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
963
+ ]),
964
+ getDefinition: (name) => getDefinition(name, environment),
965
+ });
966
+ const getEvaluationError = (body: Expression) => {
967
+ try {
968
+ evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
969
+ return undefined;
970
+ } catch (error) {
971
+ if (error instanceof TlaEvaluationError) return error;
972
+ throw error;
973
+ }
974
+ };
910
975
  substitutions.forEach((target, name) => {
911
976
  const definition = definitions.get(target);
912
- if (
977
+ const error =
913
978
  declaredConstants.includes(name) &&
914
- definition &&
915
- !isConstantLevel(definition, environment)
916
- ) {
979
+ definition?.kind === "Operator" &&
980
+ definition.params.length === 0 &&
981
+ analyzer.analyzeDefinition(definition, new Map()).level > 0
982
+ ? getEvaluationError(definition.body)
983
+ : undefined;
984
+ // TLC adds the reason unless a standard module's Java code failed.
985
+ if (error) {
917
986
  throw new TlaConfigError(
918
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
987
+ `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
919
988
  );
920
989
  }
921
990
  });
991
+ runtime.isStarted = true;
922
992
  return {
923
993
  environment,
924
994
  assumptions,
package/src/errors.ts CHANGED
@@ -1,4 +1,9 @@
1
- import { EXIT_STATUS, getExitCode, setModuleExitCode } from "./exit-codes";
1
+ import { EXIT_STATUS, getExitCode } from "./exit-codes";
2
+
3
+ // How TLC reports a RuntimeException it did not expect, such as one raised while computing next
4
+ // states.
5
+ export const UNEXPECTED_EXCEPTION =
6
+ "TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ";
2
7
 
3
8
  export class TlaError extends Error {
4
9
  constructor(message: string, cause?: unknown) {
@@ -41,10 +46,21 @@ export class TlaEvaluationError extends TlaError {
41
46
  // An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
42
47
  // keeps the name TlaEvaluationError, and index.ts doesn't export it.
43
48
  export class TlaModuleError extends TlaEvaluationError {
49
+ exitCode: number;
50
+
44
51
  constructor(message: string, exitCode: number = EXIT_STATUS.error) {
45
52
  super(message);
46
53
  this.name = "TlaEvaluationError";
47
- setModuleExitCode(this, exitCode);
54
+ this.exitCode = exitCode;
55
+ }
56
+ }
57
+
58
+ // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
59
+ // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
60
+ export class TlaIncompleteStateError extends TlaEvaluationError {
61
+ constructor(message: string) {
62
+ super(message);
63
+ this.name = "TlaEvaluationError";
48
64
  }
49
65
  }
50
66