tla-ts 0.0.3-dev.252cf06 → 0.0.3-dev.366c320

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.3-dev.252cf06",
3
+ "version": "0.0.3-dev.366c320",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/check.ts CHANGED
@@ -16,7 +16,7 @@ import {
16
16
  TlaModuleError,
17
17
  UNEXPECTED_EXCEPTION,
18
18
  } from "./errors";
19
- import { EXIT_STATUS, setExitCode, setNextStatePhase, withLivenessExitCode } from "./exit-codes";
19
+ import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
20
20
  import {
21
21
  describeRange,
22
22
  evaluate,
@@ -831,7 +831,10 @@ export const createSearch = (
831
831
  )
832
832
  );
833
833
  } catch (error) {
834
- throw withHeadline(error, `Evaluating action property ${property.name} failed.`);
834
+ throw withHeadline(
835
+ withNextStateExitCode(error),
836
+ `Evaluating action property ${property.name} failed.`,
837
+ );
835
838
  }
836
839
  }),
837
840
  satisfiesActionConstraints: (state, next) =>
@@ -867,6 +870,11 @@ export const createSearch = (
867
870
  };
868
871
  };
869
872
 
873
+ // TLC exits with the code of a standard module's failure only when the failure escapes the
874
+ // computation of next states. In every other phase, the phase decides the code.
875
+ const withNextStateExitCode = (error: unknown) =>
876
+ error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
877
+
870
878
  const withHeadline = (error: unknown, headline: string) =>
871
879
  error instanceof TlaEvaluationError
872
880
  ? setExitCode(
@@ -876,7 +884,6 @@ const withHeadline = (error: unknown, headline: string) =>
876
884
  : error;
877
885
 
878
886
  export const checkSequentially = (options: CheckOptions): CheckResult => {
879
- setNextStatePhase(false);
880
887
  const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
881
888
  const onPrint = options.onPrint ?? console.log;
882
889
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
@@ -1425,7 +1432,6 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1425
1432
  periodic.time += performance.now() - now;
1426
1433
  return lasso;
1427
1434
  };
1428
- setNextStatePhase(true);
1429
1435
  for (let index = 0; index < states.length && !runtime.isStopped; index++) {
1430
1436
  current.index = index;
1431
1437
  current.candidate = undefined;
@@ -1434,7 +1440,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1434
1440
  try {
1435
1441
  outcome.result = expandState(index);
1436
1442
  } catch (error) {
1437
- outcome.result = toFailure(error, getStateTrace(index));
1443
+ outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
1438
1444
  }
1439
1445
  if (outcome.result) return outcome.result;
1440
1446
  outDegrees[getOutDegreeBucket()]++;
package/src/codegen.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  import { getChildren, isLocallyResolved, type Expression } from "./ast";
2
- import { TlaEvaluationError } from "./errors";
3
2
  import {
4
- describeLocation,
5
3
  bindValue,
6
4
  expectBoolean,
7
5
  findGlobalName,
@@ -18,13 +16,16 @@ import {
18
16
  isWorthCaching,
19
17
  keepLazy,
20
18
  resolveGlobalOperator,
19
+ createPrimeOutsideActionError,
20
+ createUnassignedError,
21
+ getSortedFields,
22
+ hasDuplicateFields,
21
23
  type Context,
22
24
  type Environment,
23
25
  type Evaluator,
24
26
  } from "./evaluate";
25
27
  import {
26
28
  applyFunction,
27
- compareValues,
28
29
  createEnumeratedSet,
29
30
  createSortedFunction,
30
31
  createTuple,
@@ -82,14 +83,10 @@ const createRuntime = () => ({
82
83
  equal: valuesEqual,
83
84
  enumerate: getEnumeratedElements,
84
85
  unassigned: (name: string, expression: Expression): never => {
85
- throw new TlaEvaluationError(
86
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
87
- );
86
+ throw createUnassignedError(name, expression);
88
87
  },
89
88
  primeOutsideAction: (expression: Expression): never => {
90
- throw new TlaEvaluationError(
91
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
92
- );
89
+ throw createPrimeOutsideActionError(expression);
93
90
  },
94
91
  prime: (context: Context): Context => ({
95
92
  environment: context.environment,
@@ -260,13 +257,8 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
260
257
  case "FieldAccess":
261
258
  return `apply(${generate(expression.record)}, ${JSON.stringify(expression.field)}, ${addReference(generator, getFieldError(expression))})`;
262
259
  case "Record": {
263
- const fields = [...expression.fields].sort((left, right) =>
264
- compareValues(left.name, right.name),
265
- );
266
- const hasDuplicates = fields.some(
267
- (field, index) => index > 0 && fields[index - 1].name === field.name,
268
- );
269
- if (hasDuplicates) return generateDelegate(generator, expression, frame);
260
+ const fields = getSortedFields(expression.fields);
261
+ if (hasDuplicateFields(fields)) return generateDelegate(generator, expression, frame);
270
262
  const names = addReference(
271
263
  generator,
272
264
  fields.map((field) => field.name),
package/src/errors.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { EXIT_STATUS, getExitCode, setModuleExitCode } from "./exit-codes";
1
+ import { EXIT_STATUS, getExitCode } from "./exit-codes";
2
2
 
3
3
  // How TLC reports a RuntimeException it did not expect, such as one raised while computing next
4
4
  // states.
@@ -46,10 +46,12 @@ export class TlaEvaluationError extends TlaError {
46
46
  // An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
47
47
  // keeps the name TlaEvaluationError, and index.ts doesn't export it.
48
48
  export class TlaModuleError extends TlaEvaluationError {
49
+ exitCode: number;
50
+
49
51
  constructor(message: string, exitCode: number = EXIT_STATUS.error) {
50
52
  super(message);
51
53
  this.name = "TlaEvaluationError";
52
- setModuleExitCode(this, exitCode);
54
+ this.exitCode = exitCode;
53
55
  }
54
56
  }
55
57
 
package/src/evaluate.ts CHANGED
@@ -771,11 +771,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
771
771
  const global = findGlobalName(name, context.environment);
772
772
  if (global?.kind === "Variable") {
773
773
  const value = context.state?.[global.index];
774
- if (value === undefined) {
775
- throw new TlaEvaluationError(
776
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
777
- );
778
- }
774
+ if (value === undefined) throw createUnassignedError(name, expression);
779
775
  return value;
780
776
  }
781
777
  if (global?.kind === "Value") return global.value;
@@ -1055,11 +1051,7 @@ const compileIdentifier = (
1055
1051
  const index = global.index;
1056
1052
  return (context) => {
1057
1053
  const value = context.state?.[index];
1058
- if (value === undefined) {
1059
- throw new TlaEvaluationError(
1060
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
1061
- );
1062
- }
1054
+ if (value === undefined) throw createUnassignedError(name, expression);
1063
1055
  return value;
1064
1056
  };
1065
1057
  }
@@ -1571,11 +1563,24 @@ const compileSetComprehension = (
1571
1563
  };
1572
1564
  };
1573
1565
 
1574
- const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1575
- const fields = [...recordFields].sort((left, right) => compareValues(left.name, right.name));
1576
- const hasDuplicates = fields.some(
1577
- (field, index) => index > 0 && fields[index - 1].name === field.name,
1566
+ // TLC orders a record's fields by name.
1567
+ export const getSortedFields = (fields: RecordField[]) =>
1568
+ [...fields].sort((left, right) => compareValues(left.name, right.name));
1569
+
1570
+ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
1571
+ sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
1572
+
1573
+ export const createUnassignedError = (name: string, expression: Expression) =>
1574
+ new TlaEvaluationError(`The variable ${name} is not assigned at ${describeLocation(expression)}`);
1575
+
1576
+ export const createPrimeOutsideActionError = (expression: Expression) =>
1577
+ new TlaEvaluationError(
1578
+ `A primed expression appears outside an action at ${describeLocation(expression)}`,
1578
1579
  );
1580
+
1581
+ const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1582
+ const fields = getSortedFields(recordFields);
1583
+ const hasDuplicates = hasDuplicateFields(fields);
1579
1584
  const values = fields.map((field) => getEvaluator(field.value, environment));
1580
1585
  if (hasDuplicates) {
1581
1586
  return (context) =>
@@ -1653,11 +1658,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1653
1658
  case "Prime": {
1654
1659
  const inner = child(expression.expression);
1655
1660
  return (context) => {
1656
- if (!context.next) {
1657
- throw new TlaEvaluationError(
1658
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
1659
- );
1660
- }
1661
+ if (!context.next) throw createPrimeOutsideActionError(expression);
1661
1662
  return inner({
1662
1663
  environment: context.environment,
1663
1664
  scope: context.scope,
@@ -1737,9 +1738,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1737
1738
  case "Record":
1738
1739
  return compileRecord(expression.fields, environment);
1739
1740
  case "RecordSet": {
1740
- const fields = [...expression.fields].sort((left, right) =>
1741
- compareValues(left.name, right.name),
1742
- );
1741
+ const fields = getSortedFields(expression.fields);
1743
1742
  const names = fields.map((field) => field.name);
1744
1743
  const sets = fields.map((field) => child(field.value));
1745
1744
  return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
package/src/exit-codes.ts CHANGED
@@ -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,13 +24,7 @@ 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
30
  const withErrorExitCode = <Result>(run: () => Result) => {
package/src/tlc-ext.ts CHANGED
@@ -103,15 +103,35 @@ const getRegister = (runtime: CheckerRuntime, key: Value) => {
103
103
  return undefined;
104
104
  };
105
105
 
106
- const setRegister = (runtime: CheckerRuntime, key: Value, value: Value) => {
106
+ // TLC's TLCSet. TLCExt's TLCGetAndSet is a TLA+ definition that calls it.
107
+ export const setTlcValue = (
108
+ runtime: CheckerRuntime,
109
+ print: (text: string) => void,
110
+ key: Value,
111
+ value: Value,
112
+ ) => {
107
113
  if (typeof key === "number" && key >= 0) {
108
114
  runtime.registers.set(key, value);
109
115
  return;
110
116
  }
117
+ if (key === "exit") {
118
+ if (value === true) runtime.isStopped = true;
119
+ return;
120
+ }
121
+ if (key === "pause") {
122
+ if (value !== true) return;
123
+ print("Press enter to resume model checking.");
124
+ runtime.pause();
125
+ return;
126
+ }
111
127
  if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
112
128
  runtime.namedRegisters.set(key, value);
113
129
  return;
114
130
  }
131
+ if (typeof key === "string" && key.startsWith("-D")) {
132
+ runtime.properties.set(key.slice(2), formatValue(value));
133
+ return;
134
+ }
115
135
  throw new TlaModuleError(
116
136
  `The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
117
137
  );
@@ -195,7 +215,12 @@ export const createTlcExtOperators = (
195
215
  const key = getValue(args, 0, "TLCGetAndSet");
196
216
  const previous = getRegister(runtime, key) ?? getValue(args, 3, "TLCGetAndSet");
197
217
  const update = getOperator(args, 1, "TLCGetAndSet");
198
- setRegister(runtime, key, update.call([previous, getValue(args, 2, "TLCGetAndSet")]));
218
+ setTlcValue(
219
+ runtime,
220
+ print,
221
+ key,
222
+ update.call([previous, getValue(args, 2, "TLCGetAndSet")]),
223
+ );
199
224
  return previous;
200
225
  }),
201
226
  isStateLevel: true,
package/src/tlc-module.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  getValue,
16
16
  } from "./builtins";
17
17
  import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "./runtime";
18
- import { createActionRecord } from "./tlc-ext";
18
+ import { createActionRecord, setTlcValue } from "./tlc-ext";
19
19
  import { getRandomElement } from "./tlc-random";
20
20
  import {
21
21
  ANY_SET,
@@ -203,33 +203,8 @@ export const createTlcOperators = (
203
203
  }),
204
204
  defineStateOperator("TLCSet", 2, (args) => {
205
205
  const key = getValue(args, 0, "TLCSet");
206
- const value = getValue(args, 1, "TLCSet");
207
- if (typeof key === "number" && key >= 0) {
208
- runtime.registers.set(key, value);
209
- return true;
210
- }
211
- if (key === "exit") {
212
- if (value === true) runtime.isStopped = true;
213
- return true;
214
- }
215
- if (key === "pause") {
216
- if (value === true) {
217
- print("Press enter to resume model checking.");
218
- runtime.pause();
219
- }
220
- return true;
221
- }
222
- if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
223
- runtime.namedRegisters.set(key, value);
224
- return true;
225
- }
226
- if (typeof key === "string" && key.startsWith("-D")) {
227
- runtime.properties.set(key.slice(2), formatValue(value));
228
- return true;
229
- }
230
- throw new TlaModuleError(
231
- `The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
232
- );
206
+ setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
207
+ return true;
233
208
  }),
234
209
  defineConstant("Any", ANY_SET),
235
210
  defineOperator("RandomElement", 1, (args) =>