tla-ts 0.0.10-dev.96638cd93 → 0.0.10-dev.a17d6d9fa

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 (89) hide show
  1. package/dist/deciders.cjs +30 -30
  2. package/dist/deciders.js +30 -30
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove.cjs +60 -60
  10. package/dist/prove.js +73 -73
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +42 -42
  13. package/dist/transfer.cjs +34 -34
  14. package/dist/transfer.js +29 -29
  15. package/dist/worker.cjs +1 -1
  16. package/dist/worker.js +1 -1
  17. package/package.json +1 -1
  18. package/src/bags.ts +5 -2
  19. package/src/builtins.ts +4 -3
  20. package/src/check-failure.ts +1 -1
  21. package/src/check-trace.ts +90 -51
  22. package/src/check.ts +251 -133
  23. package/src/cli/tlc.ts +3 -2
  24. package/src/codegen.ts +18 -5
  25. package/src/dfid.ts +13 -8
  26. package/src/environment.ts +1 -0
  27. package/src/evaluate.ts +25 -9
  28. package/src/exit-codes.ts +1 -0
  29. package/src/java-hash.ts +14 -0
  30. package/src/liveness-checker.ts +63 -31
  31. package/src/liveness.ts +48 -10
  32. package/src/parser.ts +6 -1
  33. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  34. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  35. package/src/prover/smt/solver/asserted-formulas.ts +109 -63
  36. package/src/prover/smt/solver/ast.ts +5 -3
  37. package/src/prover/smt/solver/auf-solver.ts +23 -12
  38. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  39. package/src/prover/smt/solver/bound-simplifier.ts +10 -11
  40. package/src/prover/smt/solver/card2bv.ts +31 -15
  41. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -23
  42. package/src/prover/smt/solver/context.ts +24 -7
  43. package/src/prover/smt/solver/dependent-expr-state.ts +1 -1
  44. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  45. package/src/prover/smt/solver/elim-uncnstr.ts +1 -1
  46. package/src/prover/smt/solver/internalizer.ts +1 -1
  47. package/src/prover/smt/solver/lp/lp-settings.ts +4 -0
  48. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  49. package/src/prover/smt/solver/model-generator.ts +82 -42
  50. package/src/prover/smt/solver/opt-context.ts +11 -4
  51. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  52. package/src/prover/smt/solver/opt-solver.ts +3 -2
  53. package/src/prover/smt/solver/poly-rewriter.ts +1 -0
  54. package/src/prover/smt/solver/proto-model.ts +4 -2
  55. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  56. package/src/prover/smt/solver/qsat.ts +16 -6
  57. package/src/prover/smt/solver/rewriter.ts +31 -25
  58. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  59. package/src/prover/smt/solver/sat/inc-sat-solver.ts +13 -4
  60. package/src/prover/smt/solver/sat/sat-tactic.ts +9 -2
  61. package/src/prover/smt/solver/smt-kernel.ts +30 -2
  62. package/src/prover/smt/solver/smt-setup.ts +28 -4
  63. package/src/prover/smt/solver/smtlib-parser.ts +26 -3
  64. package/src/prover/smt/solver/smtlib-script.ts +34 -21
  65. package/src/prover/smt/solver/solve-eqs.ts +24 -17
  66. package/src/prover/smt/solver/solver.ts +18 -3
  67. package/src/prover/smt/solver/tactic.ts +68 -41
  68. package/src/prover/smt/solver/theory-bv.ts +11 -1
  69. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  70. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  71. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
  72. package/src/prover/smt/solver/theory-lra-propagate.ts +7 -1
  73. package/src/prover/smt/solver/theory-lra.ts +1 -0
  74. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  75. package/src/resolve.ts +120 -52
  76. package/src/runtime.ts +4 -0
  77. package/src/search.ts +117 -81
  78. package/src/simulate.ts +110 -50
  79. package/src/spec-description.ts +3 -4
  80. package/src/standard-modules.ts +15 -6
  81. package/src/subexpressions.ts +34 -9
  82. package/src/successors.ts +3 -2
  83. package/src/tlc/random.ts +2 -1
  84. package/src/tlc/replay.ts +3 -2
  85. package/src/tlc/tlc-operators.ts +7 -7
  86. package/src/tlc/tlcext-operators.ts +26 -10
  87. package/src/tlc/trace-spec.ts +6 -20
  88. package/src/typed-tier.ts +3 -2
  89. package/src/values.ts +95 -57
package/src/resolve.ts CHANGED
@@ -116,6 +116,7 @@ export const SELECTION_NAME = "$subexpression";
116
116
  export interface NullSelection {
117
117
  node: Expression;
118
118
  error: SanyError;
119
+ isEmpty: boolean;
119
120
  }
120
121
 
121
122
  interface SelectionArgument {
@@ -130,10 +131,20 @@ export const operatorSelectionErrors = new WeakMap<Expression, SanyError>();
130
131
  export const factReferences = new WeakMap<Fact[], Fact[]>();
131
132
  export const selectionArguments = new WeakMap<Expression, SelectionArgument>();
132
133
  export const nullSelections = new WeakMap<Expression, NullSelection[]>();
134
+ export const selectionSources = new WeakMap<Expression, Expression>();
133
135
  const unresolvedExpressions = new WeakSet<Expression>();
136
+ const emptyExpressions = new WeakSet<Expression>();
137
+
138
+ // SANY's failed selection is an operator application with no operands.
139
+ const isEmptyExpression = (expression: Expression): boolean =>
140
+ emptyExpressions.has(expression) || findNullError(nullSelections.get(expression)) !== undefined;
134
141
 
135
142
  const findNullError = (nulls: NullSelection[] | undefined) =>
136
- nulls?.find((selection) => unresolvedExpressions.has(selection.node))?.error;
143
+ nulls?.find((selection) =>
144
+ selection.isEmpty
145
+ ? isEmptyExpression(selection.node)
146
+ : unresolvedExpressions.has(selection.node),
147
+ )?.error;
137
148
 
138
149
  const renamedArities = new WeakMap<Expression, number>();
139
150
 
@@ -289,6 +300,7 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
289
300
  arity: declared?.arity ?? getArity(definition),
290
301
  params: declared?.params ?? getParamArities(definition),
291
302
  kind: "Definition",
303
+ range: definition.location.range,
292
304
  };
293
305
  }
294
306
  const binding = getModuleBinding(name, unit, undefined);
@@ -328,7 +340,7 @@ const getFixity = (expression: Expression) => {
328
340
  if (expression.kind !== "Apply" || expression.argumentsRange) return undefined;
329
341
  if (expression.args.length === 2) return "infix";
330
342
  if (expression.args.length !== 1) return undefined;
331
- return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
343
+ return expression.operator === "-." ? "prefix" : "postfix";
332
344
  };
333
345
 
334
346
  const BUILTIN_LOCATION = "In module --TLA+ BUILTINS--";
@@ -517,7 +529,6 @@ const checkSelection = (
517
529
  reference: Reference,
518
530
  selectors: Selector[],
519
531
  goal: Goal,
520
- scope: LocalName | undefined,
521
532
  unit: Unit,
522
533
  isFact: boolean,
523
534
  ) => {
@@ -535,6 +546,8 @@ const checkSelection = (
535
546
  isGoal: true,
536
547
  isSufficesOpen: goal.isSuffices,
537
548
  };
549
+ let params = 0;
550
+ let args = 0;
538
551
  for (const [offset, selector] of selectors.slice(1).entries()) {
539
552
  const index = offset + 1;
540
553
  const node = state.node;
@@ -616,15 +629,25 @@ const checkSelection = (
616
629
  if (["Number", "Decimal", "String"].includes(expression.kind)) {
617
630
  return report(index, "Selecting subexpression of expression that has none.");
618
631
  }
619
- const kind =
620
- expression.kind === "Identifier"
621
- ? findBinding(expression.name, scope, unit)?.kind
622
- : undefined;
623
- if (kind === "Variable" || kind?.startsWith("New")) {
632
+ if (isEmptyExpression(expression)) {
624
633
  return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
625
634
  }
626
- const sets = getBinder(expression)?.sets;
627
- if (sets && selector.kind !== "Operand") return node;
635
+ const binder = getBinder(expression);
636
+ if (binder && selector.kind !== "Operand") {
637
+ const count = selector.kind === "Bound" ? selector.args.length : 0;
638
+ if (selector.kind === "Bound" && count !== binder.names.length) {
639
+ return report(
640
+ index,
641
+ `Selector with ${count} argument(s) used for quantifier with ${binder.names.length} bound identifier(s).`,
642
+ );
643
+ }
644
+ params += binder.names.length;
645
+ args += count;
646
+ state.node = binder.body;
647
+ state.isGoal = false;
648
+ continue;
649
+ }
650
+ const sets = binder?.sets;
628
651
  const operands =
629
652
  expression.kind === "Identifier"
630
653
  ? []
@@ -665,13 +688,15 @@ const checkSelection = (
665
688
  return report(index, unexpectedNull);
666
689
  }
667
690
  }
668
- if (isFact) return state.node;
669
691
  if (state.node.kind === "AssumeProve") {
670
- return report(undefined, "Selected ASSUME/PROVE instead of expression.");
692
+ return isFact ? state.node : report(undefined, "Selected ASSUME/PROVE instead of expression.");
671
693
  }
672
694
  if (state.node.kind === "NewSymbol") {
673
- return report(undefined, "Selected a NEW declaration as an expression or operator.");
695
+ return isFact
696
+ ? state.node
697
+ : report(undefined, "Selected a NEW declaration as an expression or operator.");
674
698
  }
699
+ if (params !== args) return report(undefined, `Expected ${params} arguments but found ${args}.`);
675
700
  return state.node;
676
701
  };
677
702
 
@@ -750,7 +775,7 @@ const checkReference = (
750
775
  selectors.length > 1 && first.kind === "Name"
751
776
  ? (lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit))
752
777
  : undefined;
753
- return goal && { node: checkSelection(expression, selectors, goal, scope, unit, isFact) };
778
+ return goal && { node: checkSelection(expression, selectors, goal, unit, isFact) };
754
779
  };
755
780
 
756
781
  const getSymbolArity = (
@@ -1052,7 +1077,9 @@ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
1052
1077
  prefix === name
1053
1078
  ? undefined
1054
1079
  : (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
1055
- return binding?.kind === "Instance" ? { name: prefix, arity: binding.arity } : undefined;
1080
+ return binding?.kind === "Instance"
1081
+ ? { name: prefix, arity: binding.arity, range: binding.range }
1082
+ : undefined;
1056
1083
  };
1057
1084
 
1058
1085
  const isInstanceArityReported = (
@@ -1104,6 +1131,28 @@ const isArgumentCountReported = (
1104
1131
  return true;
1105
1132
  };
1106
1133
 
1134
+ const resolveExpressionArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
1135
+ const range = arg.location.range;
1136
+ const report = (message: string) => {
1137
+ if (range) unit.errors.push({ location: formatRange(range), message });
1138
+ };
1139
+ const selectionError = operatorSelectionErrors.get(arg);
1140
+ // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
1141
+ if (arg.kind === "Lambda" && arg.operator !== undefined) {
1142
+ report(`The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
1143
+ } else if (selectionError) {
1144
+ unit.errors.push(selectionError);
1145
+ } else if (arg.kind === "Lambda" && range) {
1146
+ unresolvedExpressions.add(arg);
1147
+ report("LAMBDA expression used where an expression is required.");
1148
+ } else if (arg.kind === "OperatorReference") {
1149
+ const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1150
+ if (argArity !== undefined) {
1151
+ report(`The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`);
1152
+ }
1153
+ } else resolveExpression(arg, scope, unit);
1154
+ };
1155
+
1107
1156
  const matchArgument = (
1108
1157
  arg: Expression,
1109
1158
  expected: number,
@@ -1116,26 +1165,18 @@ const matchArgument = (
1116
1165
  const report = (range: SourceRange | undefined, message: string) => {
1117
1166
  if (range) unit.errors.push({ location: formatRange(range), message });
1118
1167
  };
1168
+ const reportOperatorArity = (name: string, arity: number) => {
1169
+ const definition =
1170
+ findInstance(operator, scope, unit)?.range ?? findBinding(operator, scope, unit)?.range;
1171
+ if (!definition) return;
1172
+ report(
1173
+ operatorRange,
1174
+ `Operator with incorrect arity passed as argument. \nOperator '${resolveSynonym(name)}' of arity ${arity} is argument number ${index + 1} (counting from 1) to operator \`${formatRange(definition)}', \nbut an operator of arity ${expected} was expected.`,
1175
+ );
1176
+ };
1119
1177
  const range = arg.location.range;
1120
1178
  if (expected === 0) {
1121
- const selectionError = operatorSelectionErrors.get(arg);
1122
- // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
1123
- if (arg.kind === "Lambda" && arg.operator !== undefined) {
1124
- report(range, `The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
1125
- } else if (selectionError) {
1126
- unit.errors.push(selectionError);
1127
- } else if (arg.kind === "Lambda" && range) {
1128
- unresolvedExpressions.add(arg);
1129
- report(range, "LAMBDA expression used where an expression is required.");
1130
- } else if (arg.kind === "OperatorReference") {
1131
- const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1132
- if (argArity !== undefined) {
1133
- report(
1134
- range,
1135
- `The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`,
1136
- );
1137
- }
1138
- } else resolveExpression(arg, scope, unit);
1179
+ resolveExpressionArgument(arg, scope, unit);
1139
1180
  return true;
1140
1181
  }
1141
1182
  if (arg.kind === "Lambda") {
@@ -1149,6 +1190,8 @@ const matchArgument = (
1149
1190
  operatorRange,
1150
1191
  `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1151
1192
  );
1193
+ } else if (!isMatched && arg.operator !== undefined) {
1194
+ reportOperatorArity(arg.operator, arg.params.length);
1152
1195
  }
1153
1196
  return isMatched;
1154
1197
  }
@@ -1180,6 +1223,9 @@ const matchArgument = (
1180
1223
  `Expected arity ${expected} but found operator of arity ${argArity}.`,
1181
1224
  );
1182
1225
  }
1226
+ if (arg.kind === "OperatorReference" && argArity !== undefined && argArity !== expected) {
1227
+ reportOperatorArity(name, argArity);
1228
+ }
1183
1229
  // Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
1184
1230
  return argArity === expected && argParams.every((param) => param === 0);
1185
1231
  };
@@ -1189,16 +1235,19 @@ const resolveApply = (
1189
1235
  scope: LocalName | undefined,
1190
1236
  unit: Unit,
1191
1237
  ) => {
1192
- if (isInstanceArityReported(expression, scope, unit)) return;
1193
1238
  const operator = expression.operator;
1194
1239
  const callee = findBinding(operator, scope, unit);
1195
- const arity = getSymbolArity(expression, operator, scope, unit);
1196
- if (arity === undefined) return;
1197
- if (arity !== expression.args.length) {
1240
+ const arity = isInstanceArityReported(expression, scope, unit)
1241
+ ? undefined
1242
+ : getSymbolArity(expression, operator, scope, unit);
1243
+ if (arity !== undefined && arity !== expression.args.length) {
1198
1244
  unit.errors.push({
1199
1245
  location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
1200
1246
  message: `The operator ${resolveSynonym(operator)} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
1201
1247
  });
1248
+ }
1249
+ if (arity !== expression.args.length) {
1250
+ emptyExpressions.add(expression);
1202
1251
  return;
1203
1252
  }
1204
1253
  const params =
@@ -1219,7 +1268,7 @@ const resolveApply = (
1219
1268
  source?.operator ?? operator,
1220
1269
  source?.range ?? operatorRange,
1221
1270
  scope,
1222
- unit,
1271
+ isSelection ? { ...unit, isGenerated: false } : unit,
1223
1272
  );
1224
1273
  });
1225
1274
  if (
@@ -1291,8 +1340,17 @@ const resolveExpression = (
1291
1340
  ): void => {
1292
1341
  if (selections.has(expression) && !unit.isGenerated) {
1293
1342
  const failed = findNullError(nullSelections.get(expression));
1294
- if (failed) unit.errors.push(failed);
1295
- else resolveExpression(expression, scope, { ...unit, isGenerated: true });
1343
+ if (failed) {
1344
+ unit.errors.push(failed);
1345
+ return;
1346
+ }
1347
+ const source = selectionSources.get(expression);
1348
+ if (source) {
1349
+ Object.assign(expression, source);
1350
+ const arity = renamedArities.get(source);
1351
+ if (arity !== undefined) renamedArities.set(expression, arity);
1352
+ }
1353
+ resolveExpression(expression, scope, { ...unit, isGenerated: true });
1296
1354
  return;
1297
1355
  }
1298
1356
  const resolveAll = (expressions: Expression[]) =>
@@ -1308,6 +1366,7 @@ const resolveExpression = (
1308
1366
  return;
1309
1367
  case "ExceptAt":
1310
1368
  if (!unit.isInExcept && expression.location.range) {
1369
+ emptyExpressions.add(expression);
1311
1370
  unit.errors.push({
1312
1371
  location: formatRange(expression.location.range),
1313
1372
  message: "@ used where its meaning is not defined.",
@@ -1315,23 +1374,32 @@ const resolveExpression = (
1315
1374
  }
1316
1375
  return;
1317
1376
  case "Identifier": {
1318
- if (isInstanceArityReported(expression, scope, unit)) return;
1319
1377
  const name = expression.name;
1320
- const arity = getSymbolArity(expression, name, scope, unit);
1378
+ const arity = isInstanceArityReported(expression, scope, unit)
1379
+ ? undefined
1380
+ : getSymbolArity(expression, name, scope, unit);
1321
1381
  if (arity === 0) {
1322
- isArgumentCountReported(
1323
- findBinding(name, scope, unit),
1324
- name,
1325
- 0,
1326
- getSpelledNameRange(expression, name),
1327
- unit,
1328
- );
1329
- } else if (arity !== undefined) {
1382
+ const binding = findBinding(name, scope, unit);
1383
+ const kind = binding?.kind;
1384
+ // SANY's selector chooses no operand of a variable, theorem, step or NEW symbol, unlike of a constant or definition.
1385
+ if (
1386
+ kind === "Variable" ||
1387
+ kind === "Theorem" ||
1388
+ kind?.startsWith("New") ||
1389
+ isFact(name, unit)
1390
+ ) {
1391
+ emptyExpressions.add(expression);
1392
+ }
1393
+ isArgumentCountReported(binding, name, 0, getSpelledNameRange(expression, name), unit);
1394
+ return;
1395
+ }
1396
+ if (arity !== undefined) {
1330
1397
  unit.errors.push({
1331
1398
  location: formatRange(getSpelledNameRange(expression, name)),
1332
1399
  message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
1333
1400
  });
1334
1401
  }
1402
+ emptyExpressions.add(expression);
1335
1403
  return;
1336
1404
  }
1337
1405
  case "OperatorReference":
@@ -1468,7 +1536,7 @@ const resolveExpression = (
1468
1536
  checkLabel(expression, scope, unit);
1469
1537
  return;
1470
1538
  case "Subexpression":
1471
- isSubexpressionReported(expression, scope, unit);
1539
+ if (isSubexpressionReported(expression, scope, unit)) emptyExpressions.add(expression);
1472
1540
  return;
1473
1541
  }
1474
1542
  };
@@ -1978,7 +2046,7 @@ const checkSubstitutions = (
1978
2046
  const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
1979
2047
  const range = expression.location.range;
1980
2048
  if (declaration.arity === 0) {
1981
- resolveExpression(expression, scope, unit);
2049
+ resolveExpressionArgument(expression, scope, unit);
1982
2050
  return;
1983
2051
  }
1984
2052
  if (expression.kind === "Lambda") {
package/src/runtime.ts CHANGED
@@ -14,6 +14,7 @@ export interface CheckerRuntime {
14
14
  getCurrentState: () => Value[] | undefined;
15
15
  getFingerprint: (state: Value[]) => bigint;
16
16
  getRandom: () => JavaRandom;
17
+ reseedRandom: () => void;
17
18
  getSimulationValue:
18
19
  | ((key: string, state: (Value | undefined)[] | undefined) => Value | undefined)
19
20
  | undefined;
@@ -72,6 +73,9 @@ export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime
72
73
  }
73
74
  return workerRandom;
74
75
  },
76
+ reseedRandom: () => {
77
+ seededState = undefined;
78
+ },
75
79
  evaluateAtStartup: (evaluate) => {
76
80
  const wasStartup = isStartup;
77
81
  isStartup = true;