tla-ts 0.0.10-dev.11307be75 → 0.0.10-dev.22f7435d3

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 (108) hide show
  1. package/dist/deciders.cjs +31 -31
  2. package/dist/deciders.js +30 -30
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +6 -2
  5. package/dist/index.d.ts +6 -2
  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 +68 -68
  10. package/dist/prove.js +61 -61
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +44 -44
  13. package/dist/transfer.cjs +38 -38
  14. package/dist/transfer.js +29 -29
  15. package/dist/worker.cjs +7 -7
  16. package/dist/worker.js +7 -7
  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 +9 -6
  21. package/src/check-trace.ts +116 -64
  22. package/src/check.ts +343 -166
  23. package/src/cli/tlc-output.ts +1 -0
  24. package/src/cli/tlc.ts +3 -2
  25. package/src/codegen.ts +23 -8
  26. package/src/dependencies.ts +5 -4
  27. package/src/dfid.ts +13 -8
  28. package/src/environment.ts +1 -0
  29. package/src/evaluate.ts +58 -23
  30. package/src/exit-codes.ts +27 -13
  31. package/src/java-hash.ts +14 -0
  32. package/src/liveness-checker.ts +131 -81
  33. package/src/liveness.ts +48 -10
  34. package/src/parser.ts +6 -1
  35. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  36. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  37. package/src/prover/smt/solver/asserted-formulas.ts +109 -63
  38. package/src/prover/smt/solver/ast.ts +5 -3
  39. package/src/prover/smt/solver/auf-solver.ts +23 -12
  40. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  41. package/src/prover/smt/solver/bound-simplifier.ts +10 -11
  42. package/src/prover/smt/solver/card2bv.ts +34 -15
  43. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -23
  44. package/src/prover/smt/solver/context.ts +44 -11
  45. package/src/prover/smt/solver/dependent-expr-state.ts +19 -13
  46. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  47. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  48. package/src/prover/smt/solver/elim-uncnstr.ts +1 -1
  49. package/src/prover/smt/solver/fd-solver.ts +146 -39
  50. package/src/prover/smt/solver/internalizer.ts +1 -1
  51. package/src/prover/smt/solver/lp/lp-settings.ts +4 -0
  52. package/src/prover/smt/solver/maxcore.ts +2 -1
  53. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  54. package/src/prover/smt/solver/model-checker.ts +1 -1
  55. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  56. package/src/prover/smt/solver/model-generator.ts +82 -42
  57. package/src/prover/smt/solver/muz/dl-context.ts +12 -40
  58. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  59. package/src/prover/smt/solver/muz/dl-rule.ts +6 -7
  60. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  61. package/src/prover/smt/solver/muz/horn-tactic.ts +14 -1
  62. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  63. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  64. package/src/prover/smt/solver/opt-context.ts +12 -4
  65. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  66. package/src/prover/smt/solver/opt-solver.ts +4 -3
  67. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  68. package/src/prover/smt/solver/poly-rewriter.ts +1 -0
  69. package/src/prover/smt/solver/proto-model.ts +4 -2
  70. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  71. package/src/prover/smt/solver/qsat.ts +23 -8
  72. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  73. package/src/prover/smt/solver/rewriter.ts +31 -25
  74. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  75. package/src/prover/smt/solver/sat/inc-sat-solver.ts +56 -28
  76. package/src/prover/smt/solver/sat/sat-tactic.ts +13 -4
  77. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  78. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  79. package/src/prover/smt/solver/smt-kernel.ts +39 -4
  80. package/src/prover/smt/solver/smt-setup.ts +28 -4
  81. package/src/prover/smt/solver/smtlib-parser.ts +31 -4
  82. package/src/prover/smt/solver/smtlib-script.ts +219 -82
  83. package/src/prover/smt/solver/solve-eqs.ts +24 -17
  84. package/src/prover/smt/solver/solver.ts +18 -3
  85. package/src/prover/smt/solver/tactic.ts +132 -78
  86. package/src/prover/smt/solver/theory-bv.ts +11 -1
  87. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  88. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  89. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
  90. package/src/prover/smt/solver/theory-lra-propagate.ts +7 -1
  91. package/src/prover/smt/solver/theory-lra.ts +1 -0
  92. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  93. package/src/resolve.ts +242 -80
  94. package/src/runtime.ts +4 -0
  95. package/src/search.ts +161 -92
  96. package/src/simulate.ts +165 -102
  97. package/src/spec-description.ts +3 -4
  98. package/src/standard-modules.ts +56 -22
  99. package/src/subexpressions.ts +71 -16
  100. package/src/successors.ts +6 -3
  101. package/src/symmetry.ts +1 -0
  102. package/src/tlc/random.ts +2 -1
  103. package/src/tlc/replay.ts +22 -14
  104. package/src/tlc/tlc-operators.ts +7 -7
  105. package/src/tlc/tlcext-operators.ts +26 -10
  106. package/src/tlc/trace-spec.ts +6 -20
  107. package/src/typed-tier.ts +9 -4
  108. package/src/values.ts +95 -57
package/src/resolve.ts CHANGED
@@ -97,6 +97,7 @@ export interface SelectionArguments {
97
97
 
98
98
  interface SelectionError {
99
99
  applications: SelectionArguments[];
100
+ nulls: NullSelection[];
100
101
  error: SanyError;
101
102
  }
102
103
 
@@ -110,17 +111,40 @@ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" }
110
111
 
111
112
  export const LOCAL_RESOLUTION = { kind: "Local" } as const;
112
113
 
114
+ export const SELECTION_NAME = "$subexpression";
115
+
113
116
  export interface NullSelection {
114
117
  node: Expression;
115
118
  error: SanyError;
119
+ isEmpty: boolean;
120
+ }
121
+
122
+ interface SelectionArgument {
123
+ operator: string;
124
+ index: number;
125
+ range: SourceRange | undefined;
116
126
  }
117
127
 
118
128
  export const selections = new WeakSet<Expression>();
119
129
  export const selectionErrors = new WeakMap<Expression, SelectionError>();
120
130
  export const operatorSelectionErrors = new WeakMap<Expression, SanyError>();
121
131
  export const factReferences = new WeakMap<Fact[], Fact[]>();
132
+ export const selectionArguments = new WeakMap<Expression, SelectionArgument>();
122
133
  export const nullSelections = new WeakMap<Expression, NullSelection[]>();
134
+ export const selectionSources = new WeakMap<Expression, Expression>();
123
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;
141
+
142
+ const findNullError = (nulls: NullSelection[] | undefined) =>
143
+ nulls?.find((selection) =>
144
+ selection.isEmpty
145
+ ? isEmptyExpression(selection.node)
146
+ : unresolvedExpressions.has(selection.node),
147
+ )?.error;
124
148
 
125
149
  const renamedArities = new WeakMap<Expression, number>();
126
150
 
@@ -276,10 +300,13 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
276
300
  arity: declared?.arity ?? getArity(definition),
277
301
  params: declared?.params ?? getParamArities(definition),
278
302
  kind: "Definition",
303
+ range: definition.location.range,
279
304
  };
280
305
  }
281
306
  const binding = getModuleBinding(name, unit, undefined);
282
- if (binding?.isPending) return { key: name, arity: binding.arity, kind: binding.kind };
307
+ if (binding?.isPending) {
308
+ return { key: name, arity: binding.arity, params: binding.params, kind: binding.kind };
309
+ }
283
310
  return binding ? unit.imported.get(name) : undefined;
284
311
  };
285
312
 
@@ -313,7 +340,7 @@ const getFixity = (expression: Expression) => {
313
340
  if (expression.kind !== "Apply" || expression.argumentsRange) return undefined;
314
341
  if (expression.args.length === 2) return "infix";
315
342
  if (expression.args.length !== 1) return undefined;
316
- return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
343
+ return expression.operator === "-." ? "prefix" : "postfix";
317
344
  };
318
345
 
319
346
  const BUILTIN_LOCATION = "In module --TLA+ BUILTINS--";
@@ -502,7 +529,6 @@ const checkSelection = (
502
529
  reference: Reference,
503
530
  selectors: Selector[],
504
531
  goal: Goal,
505
- scope: LocalName | undefined,
506
532
  unit: Unit,
507
533
  isFact: boolean,
508
534
  ) => {
@@ -520,6 +546,8 @@ const checkSelection = (
520
546
  isGoal: true,
521
547
  isSufficesOpen: goal.isSuffices,
522
548
  };
549
+ let params = 0;
550
+ let args = 0;
523
551
  for (const [offset, selector] of selectors.slice(1).entries()) {
524
552
  const index = offset + 1;
525
553
  const node = state.node;
@@ -576,6 +604,7 @@ const checkSelection = (
576
604
  : "!(...)";
577
605
  const getPosition = (count: number) =>
578
606
  selector.kind === "Operand" ? getOperandIndex(selector.operand, count) : -1;
607
+ const unexpectedNull = `An unexpected null node specified by '${operand}'.\nThis is probably due to a previous error.`;
579
608
  if (state.isSufficesOpen) {
580
609
  if (getPosition(1) !== 0) {
581
610
  return report(
@@ -591,18 +620,34 @@ const checkSelection = (
591
620
  if (node.kind === "NewSymbol") return node;
592
621
  if (node.kind !== "AssumeProve") {
593
622
  const expression = getLabelBody(node);
623
+ if (unresolvedExpressions.has(expression)) {
624
+ return report(
625
+ index,
626
+ "Subexpression selection failed, probably due to an error in the selected expression.",
627
+ );
628
+ }
594
629
  if (["Number", "Decimal", "String"].includes(expression.kind)) {
595
630
  return report(index, "Selecting subexpression of expression that has none.");
596
631
  }
597
- const kind =
598
- expression.kind === "Identifier"
599
- ? findBinding(expression.name, scope, unit)?.kind
600
- : undefined;
601
- if (kind === "Variable" || kind?.startsWith("New")) {
632
+ if (isEmptyExpression(expression)) {
602
633
  return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
603
634
  }
604
- const sets = getBinder(expression)?.sets;
605
- 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;
606
651
  const operands =
607
652
  expression.kind === "Identifier"
608
653
  ? []
@@ -610,14 +655,17 @@ const checkSelection = (
610
655
  ? sets.length > 0
611
656
  ? sets
612
657
  : undefined
613
- : expression.kind === "Except"
614
- ? undefined
615
- : getOperands(expression);
658
+ : getOperands(expression);
616
659
  if (!operands) return node;
617
- const position = getPosition(operands.length);
660
+ const isExcept = expression.kind === "Except";
661
+ const position = getPosition(isExcept ? expression.updates.length + 1 : operands.length);
618
662
  if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
663
+ if (isExcept && position > 0) {
664
+ return report(index, "Selecting subexpression of an EXCEPT not yet implemented.");
665
+ }
619
666
  state.node = operands[position];
620
667
  state.isGoal = false;
668
+ if (unresolvedExpressions.has(state.node)) return report(index, unexpectedNull);
621
669
  continue;
622
670
  }
623
671
  const clauses = [...node.assumptions, node.conclusion];
@@ -632,14 +680,23 @@ const checkSelection = (
632
680
  }
633
681
  state.node = clause;
634
682
  state.isGoal = false;
683
+ if (
684
+ clause.kind !== "NewSymbol" &&
685
+ clause.kind !== "AssumeProve" &&
686
+ unresolvedExpressions.has(clause)
687
+ ) {
688
+ return report(index, unexpectedNull);
689
+ }
635
690
  }
636
- if (isFact) return state.node;
637
691
  if (state.node.kind === "AssumeProve") {
638
- return report(undefined, "Selected ASSUME/PROVE instead of expression.");
692
+ return isFact ? state.node : report(undefined, "Selected ASSUME/PROVE instead of expression.");
639
693
  }
640
694
  if (state.node.kind === "NewSymbol") {
641
- 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.");
642
698
  }
699
+ if (params !== args) return report(undefined, `Expected ${params} arguments but found ${args}.`);
643
700
  return state.node;
644
701
  };
645
702
 
@@ -718,7 +775,7 @@ const checkReference = (
718
775
  selectors.length > 1 && first.kind === "Name"
719
776
  ? (lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit))
720
777
  : undefined;
721
- return goal && { node: checkSelection(expression, selectors, goal, scope, unit, isFact) };
778
+ return goal && { node: checkSelection(expression, selectors, goal, unit, isFact) };
722
779
  };
723
780
 
724
781
  const getSymbolArity = (
@@ -772,6 +829,22 @@ const getSymbolArity = (
772
829
  );
773
830
  return undefined;
774
831
  }
832
+ if (
833
+ expression.kind !== "OperatorReference" &&
834
+ prefix !== "" &&
835
+ isPendingSelectionReported(
836
+ prefix,
837
+ expression.kind === "Apply" && expression.argumentCounts
838
+ ? expression.args.slice(0, expression.argumentCounts[0])
839
+ : [],
840
+ expression.selectorRanges?.[0],
841
+ (expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ?? location.range,
842
+ scope,
843
+ unit,
844
+ )
845
+ ) {
846
+ return undefined;
847
+ }
775
848
  if (selected || (prefix !== "" && isFact(prefix, unit))) {
776
849
  return expression.kind === "Apply" ? expression.args.length : 0;
777
850
  }
@@ -1004,7 +1077,9 @@ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
1004
1077
  prefix === name
1005
1078
  ? undefined
1006
1079
  : (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
1007
- 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;
1008
1083
  };
1009
1084
 
1010
1085
  const isInstanceArityReported = (
@@ -1056,6 +1131,28 @@ const isArgumentCountReported = (
1056
1131
  return true;
1057
1132
  };
1058
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
+
1059
1156
  const matchArgument = (
1060
1157
  arg: Expression,
1061
1158
  expected: number,
@@ -1068,25 +1165,18 @@ const matchArgument = (
1068
1165
  const report = (range: SourceRange | undefined, message: string) => {
1069
1166
  if (range) unit.errors.push({ location: formatRange(range), message });
1070
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
+ };
1071
1177
  const range = arg.location.range;
1072
1178
  if (expected === 0) {
1073
- const selectionError = operatorSelectionErrors.get(arg);
1074
- // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
1075
- if (arg.kind === "Lambda" && arg.operator !== undefined) {
1076
- report(range, `The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
1077
- } else if (selectionError) {
1078
- unit.errors.push(selectionError);
1079
- } else if (arg.kind === "Lambda" && range) {
1080
- report(range, "LAMBDA expression used where an expression is required.");
1081
- } else if (arg.kind === "OperatorReference") {
1082
- const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1083
- if (argArity !== undefined) {
1084
- report(
1085
- range,
1086
- `The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`,
1087
- );
1088
- }
1089
- } else resolveExpression(arg, scope, unit);
1179
+ resolveExpressionArgument(arg, scope, unit);
1090
1180
  return true;
1091
1181
  }
1092
1182
  if (arg.kind === "Lambda") {
@@ -1100,6 +1190,8 @@ const matchArgument = (
1100
1190
  operatorRange,
1101
1191
  `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1102
1192
  );
1193
+ } else if (!isMatched && arg.operator !== undefined) {
1194
+ reportOperatorArity(arg.operator, arg.params.length);
1103
1195
  }
1104
1196
  return isMatched;
1105
1197
  }
@@ -1131,6 +1223,9 @@ const matchArgument = (
1131
1223
  `Expected arity ${expected} but found operator of arity ${argArity}.`,
1132
1224
  );
1133
1225
  }
1226
+ if (arg.kind === "OperatorReference" && argArity !== undefined && argArity !== expected) {
1227
+ reportOperatorArity(name, argArity);
1228
+ }
1134
1229
  // Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
1135
1230
  return argArity === expected && argParams.every((param) => param === 0);
1136
1231
  };
@@ -1140,16 +1235,19 @@ const resolveApply = (
1140
1235
  scope: LocalName | undefined,
1141
1236
  unit: Unit,
1142
1237
  ) => {
1143
- if (isInstanceArityReported(expression, scope, unit)) return;
1144
1238
  const operator = expression.operator;
1145
1239
  const callee = findBinding(operator, scope, unit);
1146
- const arity = getSymbolArity(expression, operator, scope, unit);
1147
- if (arity === undefined) return;
1148
- 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) {
1149
1244
  unit.errors.push({
1150
1245
  location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
1151
1246
  message: `The operator ${resolveSynonym(operator)} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
1152
1247
  });
1248
+ }
1249
+ if (arity !== expression.args.length) {
1250
+ emptyExpressions.add(expression);
1153
1251
  return;
1154
1252
  }
1155
1253
  const params =
@@ -1160,11 +1258,19 @@ const resolveApply = (
1160
1258
  }
1161
1259
  const operatorRange =
1162
1260
  expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator);
1163
- const matches = expression.args
1164
- .slice(0, params.length)
1165
- .map((arg, index) =>
1166
- matchArgument(arg, params[index], index, operator, operatorRange, scope, unit),
1261
+ const isSelection = operator === SELECTION_NAME;
1262
+ const matches = expression.args.slice(0, params.length).map((arg, index) => {
1263
+ const source = isSelection ? selectionArguments.get(arg) : undefined;
1264
+ return matchArgument(
1265
+ arg,
1266
+ params[index],
1267
+ source?.index ?? index,
1268
+ source?.operator ?? operator,
1269
+ source?.range ?? operatorRange,
1270
+ scope,
1271
+ isSelection ? { ...unit, isGenerated: false } : unit,
1167
1272
  );
1273
+ });
1168
1274
  if (
1169
1275
  isArgumentCountReported(
1170
1276
  callee,
@@ -1180,7 +1286,7 @@ const resolveApply = (
1180
1286
  if (!isMatched && expression.location.range) {
1181
1287
  unit.errors.push({
1182
1288
  location: formatRange(expression.location.range),
1183
- message: `Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
1289
+ message: `Argument number ${index + 1} to operator '${isSelection ? "LAMBDA" : operator}' \nshould be a ${params[index]}-parameter operator.`,
1184
1290
  });
1185
1291
  }
1186
1292
  });
@@ -1209,7 +1315,7 @@ const isSelectionReported = (expression: Expression, scope: LocalName | undefine
1209
1315
  ),
1210
1316
  ),
1211
1317
  );
1212
- unit.errors.push(failed.error);
1318
+ unit.errors.push(findNullError(failed.nulls) ?? failed.error);
1213
1319
  return true;
1214
1320
  };
1215
1321
 
@@ -1233,11 +1339,18 @@ const resolveExpression = (
1233
1339
  unit: Unit,
1234
1340
  ): void => {
1235
1341
  if (selections.has(expression) && !unit.isGenerated) {
1236
- const failed = nullSelections
1237
- .get(expression)
1238
- ?.find((selection) => unresolvedExpressions.has(selection.node));
1239
- if (failed) unit.errors.push(failed.error);
1240
- else resolveExpression(expression, scope, { ...unit, isGenerated: true });
1342
+ const failed = findNullError(nullSelections.get(expression));
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 });
1241
1354
  return;
1242
1355
  }
1243
1356
  const resolveAll = (expressions: Expression[]) =>
@@ -1253,6 +1366,7 @@ const resolveExpression = (
1253
1366
  return;
1254
1367
  case "ExceptAt":
1255
1368
  if (!unit.isInExcept && expression.location.range) {
1369
+ emptyExpressions.add(expression);
1256
1370
  unit.errors.push({
1257
1371
  location: formatRange(expression.location.range),
1258
1372
  message: "@ used where its meaning is not defined.",
@@ -1260,23 +1374,32 @@ const resolveExpression = (
1260
1374
  }
1261
1375
  return;
1262
1376
  case "Identifier": {
1263
- if (isInstanceArityReported(expression, scope, unit)) return;
1264
1377
  const name = expression.name;
1265
- const arity = getSymbolArity(expression, name, scope, unit);
1378
+ const arity = isInstanceArityReported(expression, scope, unit)
1379
+ ? undefined
1380
+ : getSymbolArity(expression, name, scope, unit);
1266
1381
  if (arity === 0) {
1267
- isArgumentCountReported(
1268
- findBinding(name, scope, unit),
1269
- name,
1270
- 0,
1271
- getSpelledNameRange(expression, name),
1272
- unit,
1273
- );
1274
- } 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) {
1275
1397
  unit.errors.push({
1276
1398
  location: formatRange(getSpelledNameRange(expression, name)),
1277
1399
  message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
1278
1400
  });
1279
1401
  }
1402
+ emptyExpressions.add(expression);
1280
1403
  return;
1281
1404
  }
1282
1405
  case "OperatorReference":
@@ -1288,6 +1411,7 @@ const resolveExpression = (
1288
1411
  case "Lambda":
1289
1412
  if (selections.has(expression)) resolveLambda(expression, scope, unit);
1290
1413
  else if (expression.location.range) {
1414
+ unresolvedExpressions.add(expression);
1291
1415
  unit.errors.push({
1292
1416
  location: formatRange(expression.location.range),
1293
1417
  message: "LAMBDA expression used where an expression is required.",
@@ -1412,11 +1536,36 @@ const resolveExpression = (
1412
1536
  checkLabel(expression, scope, unit);
1413
1537
  return;
1414
1538
  case "Subexpression":
1415
- isSubexpressionReported(expression, scope, unit);
1539
+ if (isSubexpressionReported(expression, scope, unit)) emptyExpressions.add(expression);
1416
1540
  return;
1417
1541
  }
1418
1542
  };
1419
1543
 
1544
+ const isPendingSelectionReported = (
1545
+ name: string,
1546
+ args: Expression[],
1547
+ selectorRange: SourceRange | undefined,
1548
+ argumentsRange: SourceRange | undefined,
1549
+ scope: LocalName | undefined,
1550
+ unit: Unit,
1551
+ ) => {
1552
+ const pending = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
1553
+ if (!pending?.isPending || !selectorRange || !argumentsRange) return false;
1554
+ if (args.length !== pending.arity) {
1555
+ unit.errors.push({
1556
+ location: formatRange(argumentsRange),
1557
+ message: `The operator ${name} requires ${pending.arity} arguments.`,
1558
+ });
1559
+ return true;
1560
+ }
1561
+ args.forEach((arg) => resolveExpression(arg, scope, unit));
1562
+ unit.errors.push({
1563
+ location: formatRange(selectorRange),
1564
+ message: `Subexpression of \`${name}' used inside the operator's definition.`,
1565
+ });
1566
+ return true;
1567
+ };
1568
+
1420
1569
  const isSubexpressionReported = (
1421
1570
  expression: Expression & { kind: "Subexpression" },
1422
1571
  scope: LocalName | undefined,
@@ -1428,11 +1577,6 @@ const isSubexpressionReported = (
1428
1577
  ) {
1429
1578
  return true;
1430
1579
  }
1431
- expression.selectors
1432
- .flatMap((selector) => ("args" in selector ? selector.args : []))
1433
- .forEach((arg) => resolveArgument(arg, scope, unit));
1434
- const selected = checkReference(expression, scope, unit, false);
1435
- if (selected) return selected.node === undefined;
1436
1580
  const head = expression.selectors[0];
1437
1581
  const range = expression.selectorRanges?.[0];
1438
1582
  const nameCount = expression.selectors.findIndex((selector) => selector.kind !== "Name");
@@ -1467,21 +1611,39 @@ const isSubexpressionReported = (
1467
1611
  });
1468
1612
  return true;
1469
1613
  }
1470
- if (head.kind !== "Name" || expression.selectors[1].kind === "Name" || !range) return false;
1471
- const local = lookupLocal(scope, head.name);
1472
- const binding = local ?? findSymbol(head.name, unit);
1473
- if ((binding === undefined && !isFact(head.name, unit)) || binding?.kind === "Instance") {
1474
- unit.errors.push({
1475
- location: formatRange(range),
1476
- message: `Unknown operator: \`${resolveSynonym(head.name)}'.`,
1477
- });
1478
- return true;
1614
+ if (head.kind === "Name" && expression.selectors[1].kind !== "Name" && range) {
1615
+ const local = lookupLocal(scope, head.name);
1616
+ const binding = local ?? findSymbol(head.name, unit);
1617
+ if ((binding === undefined && !isFact(head.name, unit)) || binding?.kind === "Instance") {
1618
+ unit.errors.push({
1619
+ location: formatRange(range),
1620
+ message: `Unknown operator: \`${resolveSynonym(head.name)}'.`,
1621
+ });
1622
+ return true;
1623
+ }
1624
+ if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.isNonFact)) {
1625
+ reportUnselectable(head.name, range, unit);
1626
+ return true;
1627
+ }
1479
1628
  }
1480
- if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.isNonFact)) {
1481
- reportUnselectable(head.name, range, unit);
1629
+ if (
1630
+ head.kind === "Name" &&
1631
+ isPendingSelectionReported(
1632
+ head.name,
1633
+ head.args,
1634
+ range,
1635
+ expression.argumentRanges?.[0] ?? expression.location.range,
1636
+ scope,
1637
+ unit,
1638
+ )
1639
+ ) {
1482
1640
  return true;
1483
1641
  }
1484
- return false;
1642
+ expression.selectors
1643
+ .flatMap((selector) => ("args" in selector ? selector.args : []))
1644
+ .forEach((arg) => resolveArgument(arg, scope, unit));
1645
+ const selected = checkReference(expression, scope, unit, false);
1646
+ return selected ? selected.node === undefined : false;
1485
1647
  };
1486
1648
 
1487
1649
  const checkFact = (fact: Fact, reference: Fact, scope: LocalName | undefined, unit: Unit) => {
@@ -1884,7 +2046,7 @@ const checkSubstitutions = (
1884
2046
  const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
1885
2047
  const range = expression.location.range;
1886
2048
  if (declaration.arity === 0) {
1887
- resolveExpression(expression, scope, unit);
2049
+ resolveExpressionArgument(expression, scope, unit);
1888
2050
  return;
1889
2051
  }
1890
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;