tla-ts 0.0.10-dev.e3e5dd576 → 0.0.10-dev.e5c8dc315

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 (60) hide show
  1. package/dist/deciders.cjs +1 -1
  2. package/dist/deciders.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +10 -10
  8. package/dist/prove.js +57 -57
  9. package/dist/run.cjs +26 -26
  10. package/dist/run.js +32 -32
  11. package/dist/transfer.cjs +94 -94
  12. package/dist/transfer.js +83 -83
  13. package/package.json +1 -1
  14. package/src/ast.ts +16 -0
  15. package/src/check-trace.ts +43 -42
  16. package/src/check.ts +2 -1
  17. package/src/coverage.ts +66 -18
  18. package/src/environment.ts +34 -30
  19. package/src/evaluate.ts +57 -39
  20. package/src/instances.ts +17 -14
  21. package/src/liveness.ts +4 -0
  22. package/src/prover/smt/solver/ackermannize-bv-tactic.ts +3 -2
  23. package/src/prover/smt/solver/bit-blast-tactic.ts +1 -1
  24. package/src/prover/smt/solver/bv1-blast.ts +1 -1
  25. package/src/prover/smt/solver/card2bv.ts +1 -1
  26. package/src/prover/smt/solver/ctx-simplify.ts +8 -2
  27. package/src/prover/smt/solver/diff-neq.ts +8 -6
  28. package/src/prover/smt/solver/dt2bv-tactic.ts +10 -8
  29. package/src/prover/smt/solver/elim-uncnstr.ts +26 -20
  30. package/src/prover/smt/solver/eq2bv-tactic.ts +15 -6
  31. package/src/prover/smt/solver/fd-solver.ts +1 -1
  32. package/src/prover/smt/solver/fix-dl-var.ts +1 -1
  33. package/src/prover/smt/solver/fm-tactic.ts +9 -7
  34. package/src/prover/smt/solver/fpa2bv-tactic.ts +1 -1
  35. package/src/prover/smt/solver/lia2pb.ts +1 -1
  36. package/src/prover/smt/solver/nlqsat.ts +1 -0
  37. package/src/prover/smt/solver/normalize-bounds.ts +2 -2
  38. package/src/prover/smt/solver/occf.ts +1 -1
  39. package/src/prover/smt/solver/pb2bv-tactic.ts +9 -3
  40. package/src/prover/smt/solver/purify-arith.ts +51 -49
  41. package/src/prover/smt/solver/reduce-args.ts +2 -2
  42. package/src/prover/smt/solver/sat/goal2sat.ts +12 -2
  43. package/src/prover/smt/solver/sat/inc-sat-solver.ts +21 -5
  44. package/src/prover/smt/solver/sat/sat-tactic.ts +51 -32
  45. package/src/prover/smt/solver/sat/sat2goal.ts +4 -3
  46. package/src/prover/smt/solver/smtlib-script.ts +6 -4
  47. package/src/prover/smt/solver/solver.ts +0 -1
  48. package/src/prover/smt/solver/tactic.ts +40 -5
  49. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -0
  50. package/src/prover/smt/solver/tseitin-cnf.ts +14 -5
  51. package/src/resolve.ts +54 -28
  52. package/src/runtime.ts +14 -1
  53. package/src/sany/generator-proofs.ts +1 -0
  54. package/src/sany/selectors.ts +1 -0
  55. package/src/search.ts +3 -1
  56. package/src/simulate.ts +26 -17
  57. package/src/spec-description.ts +8 -2
  58. package/src/subexpressions.ts +138 -45
  59. package/src/successors.ts +13 -7
  60. package/src/tlc/replay.ts +80 -3
package/src/resolve.ts CHANGED
@@ -147,7 +147,7 @@ const isEmptyExpression = (expression: Expression): boolean =>
147
147
  const findNullSelection = (nulls: NullSelection[] | undefined) =>
148
148
  nulls?.find((selection) =>
149
149
  selection.isEmpty
150
- ? isEmptyExpression(selection.node)
150
+ ? emptyExpressions.has(selection.node)
151
151
  : unresolvedExpressions.has(selection.node),
152
152
  );
153
153
 
@@ -447,13 +447,30 @@ export const getBinder = (
447
447
  }
448
448
  };
449
449
 
450
+ // SANY parses `a /\ b /\ c` as `(a /\ b) /\ c`, so an infix junction has two operands.
451
+ const getJunctionOperands = (expression: Expression & { kind: "And" | "Or" }) => {
452
+ const operands = expression.operands;
453
+ const range = expression.location.range;
454
+ const end = expression.prefixEnds?.[operands.length - 3];
455
+ if (expression.isBulleted || !range || !end) return operands;
456
+ return [
457
+ {
458
+ ...expression,
459
+ operands: operands.slice(0, -1),
460
+ prefixEnds: expression.prefixEnds?.slice(0, -1),
461
+ location: { ...expression.location, range: { ...range, end } },
462
+ },
463
+ operands[operands.length - 1],
464
+ ];
465
+ };
466
+
450
467
  export const getOperands = (expression: Expression): Expression[] | undefined => {
451
468
  switch (expression.kind) {
452
469
  case "Apply":
453
470
  return expression.args;
454
471
  case "And":
455
472
  case "Or":
456
- return expression.operands;
473
+ return getJunctionOperands(expression);
457
474
  case "If":
458
475
  return [expression.condition, expression.consequent, expression.alternative];
459
476
  case "Tuple":
@@ -1152,16 +1169,17 @@ const resolveDefinitions = (
1152
1169
  return resolvedScope;
1153
1170
  };
1154
1171
 
1155
- const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
1156
- const prefix = name.split("!")[0];
1157
- const binding =
1158
- prefix === name
1159
- ? undefined
1160
- : (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
1161
- return binding?.kind === "Instance"
1162
- ? { name: prefix, arity: binding.arity, range: binding.range }
1163
- : undefined;
1164
- };
1172
+ const getInstances = (names: string[], scope: LocalName | undefined, unit: Unit) =>
1173
+ names.slice(0, -1).map((_name, index) => {
1174
+ const prefix = names.slice(0, index + 1).join("!");
1175
+ const binding = lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined);
1176
+ return binding?.kind === "Instance"
1177
+ ? { name: prefix, arity: binding.arity, range: binding.range }
1178
+ : undefined;
1179
+ });
1180
+
1181
+ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
1182
+ getInstances(name.split("!"), scope, unit)[0];
1165
1183
 
1166
1184
  const isInstanceArityReported = (
1167
1185
  expression: Expression,
@@ -1171,24 +1189,28 @@ const isInstanceArityReported = (
1171
1189
  if (!isReference(expression)) return false;
1172
1190
  const selectors = getSelectors(expression);
1173
1191
  const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
1174
- const instance = findInstance(
1175
- selectors
1176
- .slice(0, nameCount === -1 ? selectors.length : nameCount)
1177
- .flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []))
1178
- .join("!"),
1192
+ const names = selectors
1193
+ .slice(0, nameCount === -1 ? selectors.length : nameCount)
1194
+ .flatMap((selector) => (selector.kind === "Name" ? [selector] : []));
1195
+ const instances = getInstances(
1196
+ names.map((name) => name.name),
1179
1197
  scope,
1180
1198
  unit,
1181
1199
  );
1182
- const first = selectors[0];
1183
- const count = first.kind === "Name" ? first.args.length : 0;
1184
- if (!instance || count === instance.arity) return false;
1200
+ const index = instances.findIndex(
1201
+ (instance, position) =>
1202
+ instance !== undefined &&
1203
+ (instances[position - 1]?.arity ?? 0) + names[position].args.length !== instance.arity,
1204
+ );
1205
+ const instance = instances[index];
1206
+ if (!instance) return false;
1185
1207
  const range =
1186
- (expression.kind === "Identifier" ? undefined : expression.argumentRanges?.[0]) ??
1208
+ (expression.kind === "Identifier" ? undefined : expression.argumentRanges?.[index]) ??
1187
1209
  expression.location.range;
1188
1210
  if (range) {
1189
1211
  unit.errors.push({
1190
1212
  location: formatRange(range),
1191
- message: `The operator ${instance.name} requires ${instance.arity} arguments.`,
1213
+ message: `The operator ${instance.name} requires ${instance.arity - (instances[index - 1]?.arity ?? 0)} arguments.`,
1192
1214
  });
1193
1215
  }
1194
1216
  return true;
@@ -1234,15 +1256,17 @@ const resolveExpressionArgument = (arg: Expression, scope: LocalName | undefined
1234
1256
  } else resolveExpression(arg, scope, unit);
1235
1257
  };
1236
1258
 
1237
- const getSelectorArgumentError = (reference: Expression) => {
1238
- // SANY reads a reference that ends in arguments as an application, not as a selector's arguments.
1259
+ const getSelectorArgumentError = (reference: Expression, isDefEntry = false) => {
1260
+ if (!isReference(reference)) return undefined;
1261
+ // SANY reads a reference that ends in arguments as an application, not as a selector's arguments, outside a DEF clause.
1239
1262
  const selectors =
1263
+ isDefEntry ||
1240
1264
  reference.kind === "Subexpression" ||
1241
1265
  (reference.kind === "Apply" && reference.argumentCounts?.at(-1) === 0)
1242
1266
  ? getSelectors(reference)
1243
1267
  : [];
1244
1268
  const index = selectors.findIndex((selector) => "args" in selector && selector.args.length > 0);
1245
- if (index === -1 || !isReference(reference)) return undefined;
1269
+ if (index === -1) return undefined;
1246
1270
  const selector = selectors[index];
1247
1271
  return {
1248
1272
  index,
@@ -1356,7 +1380,7 @@ const resolveApply = (
1356
1380
  if (arity !== undefined && arity !== expression.args.length) {
1357
1381
  unit.errors.push({
1358
1382
  location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
1359
- message: `The operator ${resolveSynonym(operator)} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
1383
+ message: `The operator ${resolveSynonym(operator)} requires ${arity - (getInstances(operator.split("!"), scope, unit).findLast((instance) => instance !== undefined)?.arity ?? 0)} arguments.`,
1360
1384
  });
1361
1385
  }
1362
1386
  if (arity !== expression.args.length) {
@@ -1461,6 +1485,7 @@ const resolveExpression = (
1461
1485
  if (selections.has(expression) && !unit.isGenerated) {
1462
1486
  const failed = findNullSelection(nullSelections.get(expression));
1463
1487
  if (failed) {
1488
+ emptyExpressions.add(expression);
1464
1489
  matchApplications(failed.applications, scope, unit);
1465
1490
  unit.errors.push(failed.error);
1466
1491
  return;
@@ -1879,10 +1904,11 @@ const isDefinitionEntry = (def: Fact, scope: LocalName | undefined, unit: Unit)
1879
1904
  const local = lookupLocal(scope, name);
1880
1905
  const binding = local ?? findSymbol(name, unit) ?? getModuleBinding(name, unit, range);
1881
1906
  if (binding === undefined && isFact(name, unit)) return true;
1907
+ // Deviation from SANY: Generator#genIdToSelector throws an internal error on the arguments of a name after a prefix, as in DEF A!B(1); tla-ts rejects them as Generator#selectorToNode rejects a selector's arguments in a DEF clause.
1882
1908
  const isDefined =
1883
1909
  isReference(reference) &&
1884
1910
  getSelectors(reference).length > 1 &&
1885
- (binding === undefined || getSelectorArgumentError(reference))
1911
+ (binding === undefined || getSelectorArgumentError(reference, true))
1886
1912
  ? isDefinitionSelected(def, reference, scope, unit)
1887
1913
  : name === "" || isDefinitionName(name, local, binding, range, unit);
1888
1914
  if (isDefined) return true;
@@ -1926,7 +1952,7 @@ const isDefinitionSelected = (
1926
1952
  if (range) unit.errors.push({ location: formatRange(range), message });
1927
1953
  return false;
1928
1954
  };
1929
- const argumentError = getSelectorArgumentError(reference);
1955
+ const argumentError = getSelectorArgumentError(reference, true);
1930
1956
  if (argumentError?.index === 0) return report(argumentError.range, argumentError.message);
1931
1957
  const selectors = getSelectors(reference);
1932
1958
  const ranges = reference.selectorRanges;
package/src/runtime.ts CHANGED
@@ -15,6 +15,7 @@ export interface CheckerRuntime {
15
15
  getFingerprint: (state: Value[]) => bigint;
16
16
  getRandom: () => JavaRandom;
17
17
  reseedRandom: () => void;
18
+ runWithNewRandom: <Result>(evaluate: () => Result) => Result;
18
19
  getSimulationValue:
19
20
  | ((key: string, state: (Value | undefined)[] | undefined) => Value | undefined)
20
21
  | undefined;
@@ -47,7 +48,7 @@ export interface CheckerRuntime {
47
48
 
48
49
  export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime => {
49
50
  const startupRandom = createJavaRandom(seed);
50
- const workerRandom = createJavaRandom(seed);
51
+ let workerRandom = createJavaRandom(seed);
51
52
  let seededState: Value[] | undefined;
52
53
  let isStartup = false;
53
54
  const runtime: CheckerRuntime = {
@@ -76,6 +77,18 @@ export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime
76
77
  reseedRandom: () => {
77
78
  seededState = undefined;
78
79
  },
80
+ runWithNewRandom: (evaluate) => {
81
+ const random = workerRandom;
82
+ const state = seededState;
83
+ workerRandom = createJavaRandom(seed);
84
+ seededState = undefined;
85
+ try {
86
+ return evaluate();
87
+ } finally {
88
+ workerRandom = random;
89
+ seededState = state;
90
+ }
91
+ },
79
92
  evaluateAtStartup: (evaluate) => {
80
93
  const wasStartup = isStartup;
81
94
  isStartup = true;
@@ -125,6 +125,7 @@ const generateDefs = (generation: ModuleGeneration, defs: Fact[]) =>
125
125
  entryErrors.forEach((error) => generation.addError(error.location, error.message));
126
126
  if (entryErrors.length > 0) return [];
127
127
  const location = getLocation(generation.file, fact);
128
+ // Deviation from SANY: Generator#genIdToSelector throws an internal error on the arguments of a name after a prefix, as in DEF A!B(1); tla-ts passes them to the selector, which rejects them.
128
129
  const symbol = generateSelector(
129
130
  generation,
130
131
  getDefEntryBinder(fact) ??
@@ -522,6 +522,7 @@ export const createSelectorResolver =
522
522
  );
523
523
  }
524
524
  if (item.op !== "Null" && item.op !== "@") {
525
+ // Deviation from SANY: Generator#selectorToNode names a numbered selector here by its syntax node, N_StructOp; tla-ts names it by its number.
525
526
  return generator.addError(
526
527
  getItemLocation(),
527
528
  `Cannot use !${item.name} to select subexpression of a LAMBDA.`,
package/src/search.ts CHANGED
@@ -71,7 +71,7 @@ import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty
71
71
  import { getLiveExpressions } from "./liveness-checker";
72
72
  import { prettyPrint } from "./pretty-print";
73
73
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
74
- import { createIncompleteSuccessorError } from "./successors";
74
+ import { createIncompleteSuccessorError, type SuccessorGenerator } from "./successors";
75
75
  import {
76
76
  getSpecDescription,
77
77
  getSpecificationValue,
@@ -1003,6 +1003,7 @@ export const walkCoveredSuccessors = (
1003
1003
  onIncomplete = (partial: State, name: string | undefined): void => {
1004
1004
  throw createIncompleteSuccessorError(partial, name, environment);
1005
1005
  },
1006
+ generator?: SuccessorGenerator,
1006
1007
  ) =>
1007
1008
  walkSuccessors(
1008
1009
  action,
@@ -1018,6 +1019,7 @@ export const walkCoveredSuccessors = (
1018
1019
  onIncomplete(partial, action.name);
1019
1020
  return true;
1020
1021
  },
1022
+ generator,
1021
1023
  );
1022
1024
 
1023
1025
  export const reportAssumptionViolation = (
package/src/simulate.ts CHANGED
@@ -500,10 +500,12 @@ export const simulate = (options: SimulationOptions): number => {
500
500
  isException: false,
501
501
  isSummarized: true,
502
502
  });
503
+ const generator = options.isGenerating ? { random: workerRandom, kept: 0 } : undefined;
503
504
  const getSuccessors = (current: TraceNode, index: number) => {
504
505
  const action = nextActions[index];
505
506
  const coveredAction = covered?.actions[index];
506
- const successors: TraceNode[] = [];
507
+ const keptSuccessors: TraceNode[] = [];
508
+ if (generator) generator.kept = 0;
507
509
  let failure: WorkerError | undefined;
508
510
  runtime.deferred = undefined;
509
511
  evaluatedAction = action;
@@ -544,33 +546,37 @@ export const simulate = (options: SimulationOptions): number => {
544
546
  search.trackStatePossible(node.state);
545
547
  if (!search.satisfiesActionConstraints(current.state, node.state)) return false;
546
548
  search.trackActionPossible(current.state, node.state);
547
- if (successors.length === getSetBound()) throw new TlaTooManyStatesError();
549
+ if (keptSuccessors.length === getSetBound()) throw new TlaTooManyStatesError();
548
550
  if (coverage && coveredAction) coverage.secondary[coveredAction.node]++;
549
- successors.push(node);
551
+ keptSuccessors.push(node);
552
+ if (generator) generator.kept++;
550
553
  return false;
551
554
  };
552
555
  try {
553
- if (coverage && coveredAction && !options.isGenerating) {
554
- walkCoveredSuccessors(coverage, coveredAction, current.state, environment, (state) => {
555
- if (visit({ state, action: action.name })) throw STOP_WALK;
556
- });
556
+ if (coverage && coveredAction) {
557
+ walkCoveredSuccessors(
558
+ coverage,
559
+ coveredAction,
560
+ current.state,
561
+ environment,
562
+ (state) => {
563
+ if (visit({ state, action: action.name })) throw STOP_WALK;
564
+ },
565
+ undefined,
566
+ generator,
567
+ );
557
568
  } else {
558
569
  forEachSuccessor(
559
570
  action.expression,
560
571
  current.state,
561
572
  environment,
562
- coverage && coveredAction
563
- ? (successor) => {
564
- coverage.primary[coveredAction.node]++;
565
- return visit(successor);
566
- }
567
- : visit,
573
+ visit,
568
574
  action.scope,
569
- options.isGenerating ? workerRandom : undefined,
575
+ generator,
570
576
  );
571
577
  }
572
578
  } catch (error) {
573
- if (error === STOP_WALK) return { successors, error: failure };
579
+ if (error === STOP_WALK) return { successors: keptSuccessors, error: failure };
574
580
  if (!(error instanceof TlaIncompleteSuccessorError)) throw error;
575
581
  const node = toNode(
576
582
  error.state.map((value) => value ?? createModelValue("null")),
@@ -584,9 +590,12 @@ export const simulate = (options: SimulationOptions): number => {
584
590
  nextActions.length,
585
591
  options.version,
586
592
  );
587
- return { successors, error: toWorkerError(MESSAGE_CODE.incompleteSuccessor, text, node) };
593
+ return {
594
+ successors: keptSuccessors,
595
+ error: toWorkerError(MESSAGE_CODE.incompleteSuccessor, text, node),
596
+ };
588
597
  }
589
- return { successors, error: failure };
598
+ return { successors: keptSuccessors, error: failure };
590
599
  };
591
600
  let currentNode = initialStates[0];
592
601
  const scheduler = createScheduler(
@@ -27,6 +27,7 @@ import {
27
27
  import { ACTION_LEVEL, CONSTANT_LEVEL, TEMPORAL_LEVEL, VARIABLE_LEVEL } from "./levels";
28
28
  import { bindLetDefinitions, getLevel } from "./liveness";
29
29
  import { createRecord, createSet, createTuple, formatValue, type Value } from "./values";
30
+ import { SELECTION_NAME } from "./resolve";
30
31
  import { LITERAL_KINDS } from "./tlc/replay";
31
32
  import type { TlcVersion } from "./tlc/version";
32
33
 
@@ -275,7 +276,10 @@ const getActions = (
275
276
  case "Label":
276
277
  return getActions(expression.body, scope, closure, environment, instanceActions);
277
278
  case "Selection":
278
- return getActions(expression.expression, scope, closure, environment, instanceActions);
279
+ // TLC's Tool#getActions takes SANY's $Nop as an action of its own.
280
+ return expression.location.range === undefined
281
+ ? getActions(expression.expression, scope, closure, environment, instanceActions)
282
+ : [createSplitAction()];
279
283
  case "Or":
280
284
  return expression.operands.flatMap((operand) =>
281
285
  getActions(operand, scope, closure, environment, instanceActions),
@@ -308,7 +312,9 @@ const getActions = (
308
312
  if (!called) return [createSplitAction()];
309
313
  const args = getArguments(expression);
310
314
  const calledName = isLocallyResolved(expression)
311
- ? called.name
315
+ ? called.name === SELECTION_NAME
316
+ ? "LAMBDA"
317
+ : called.name
312
318
  : (environment.instanceNames.get(getOperatorName(expression) ?? "") ?? called.name);
313
319
  const callAction = (): SpecAction[] => [
314
320
  { name: calledName, location: expression.location, parameters: [], expression, scope },