tla-ts 0.0.3-dev.e927a09 → 0.0.3-dev.fb38eba

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.e927a09",
3
+ "version": "0.0.3-dev.fb38eba",
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/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/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/run-cli.ts CHANGED
@@ -19,12 +19,14 @@ import {
19
19
  TlaError,
20
20
  TlaEvaluationError,
21
21
  TlaProofError,
22
+ TlaSyntaxError,
22
23
  } from "./errors";
23
24
  import { EXIT_STATUS } from "./exit-codes";
24
25
  import type { LineRange } from "./obligation-generate";
25
26
  import { getAtPrefix, getLocusText } from "./obligation-expression";
26
27
  import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
27
28
  import { check } from "./parallel";
29
+ import { parseModule } from "./parser";
28
30
  import { prettyPrint } from "./pretty-print";
29
31
  import {
30
32
  formatProbability,
@@ -198,6 +200,14 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
198
200
  console.error(`Cannot find the specification file ${specPath}`);
199
201
  return EXIT_STATUS.configParseError;
200
202
  }
203
+ const source = readFileSync(specPath, "utf8");
204
+ const fileName = basename(specPath, ".tla");
205
+ const moduleName = parseModule(source).name;
206
+ if (moduleName !== fileName) {
207
+ throw new TlaSyntaxError(
208
+ `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
209
+ );
210
+ }
201
211
  const configPath =
202
212
  args.get("-config")?.[0] ??
203
213
  (args.has("-config") ? undefined : specPath.replace(/\.tla$/, ".cfg"));
@@ -227,7 +237,7 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
227
237
  workers: getWorkerCount(args.get("-workers")),
228
238
  livenessCheck: ["final", "seqfinal", "off"].includes(livenessCheck) ? "final" : "default",
229
239
  seed: BigInt(seed),
230
- source: readFileSync(specPath, "utf8"),
240
+ source,
231
241
  config,
232
242
  onPause: () => readSync(0, Buffer.alloc(1)),
233
243
  loadModule: (name) => {
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) =>