tla-ts 0.0.10-dev.68b2c4968 → 0.0.10-dev.6b2a6741a

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 (50) hide show
  1. package/dist/deciders.cjs +1 -1
  2. package/dist/deciders.js +1 -1
  3. package/dist/index.d.cts +1 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/prove.cjs +6 -6
  6. package/dist/prove.js +6 -6
  7. package/dist/run.cjs +7 -7
  8. package/dist/run.js +7 -7
  9. package/dist/transfer.cjs +33 -33
  10. package/dist/transfer.js +29 -29
  11. package/package.json +1 -1
  12. package/src/ast.ts +1 -0
  13. package/src/cli/xml-exporter.ts +1 -1
  14. package/src/codegen.ts +3 -3
  15. package/src/environment.ts +40 -33
  16. package/src/instances.ts +4 -4
  17. package/src/java-overrides.ts +2 -2
  18. package/src/levels.ts +4 -4
  19. package/src/obligations/index.ts +1 -1
  20. package/src/obligations/normalize.ts +1 -4
  21. package/src/obligations/printer.ts +5 -5
  22. package/src/parser.ts +35 -106
  23. package/src/prover/isabelle/isar/attrib.ts +4 -4
  24. package/src/prover/isabelle/pure/rewriter.ts +3 -3
  25. package/src/prover/isabelle/pure/term.ts +3 -3
  26. package/src/prover/prep/md5.ts +26 -29
  27. package/src/prover/smt/solver/logic-tactics.ts +2 -2
  28. package/src/prover/smt/solver/opt-preprocess.ts +1 -1
  29. package/src/prover/smt/solver/seq-decl-plugin.ts +1 -1
  30. package/src/prover/smt/solver/seq-rewriter.ts +1 -1
  31. package/src/prover/smt/solver/tactic.ts +4 -4
  32. package/src/prover/tlapm/cache.ts +8 -8
  33. package/src/resolve.ts +374 -125
  34. package/src/runtime.ts +1 -1
  35. package/src/sany/builtins.ts +10 -10
  36. package/src/sany/generation.ts +1 -0
  37. package/src/sany/generator-definitions.ts +1 -0
  38. package/src/sany/generator-expressions.ts +6 -3
  39. package/src/sany/generator-proofs.ts +4 -2
  40. package/src/sany/lexer.ts +3 -3
  41. package/src/sany/modules.ts +4 -2
  42. package/src/sany/selectors.ts +5 -1
  43. package/src/sany/xml.ts +2 -3
  44. package/src/subexpressions.ts +102 -18
  45. package/src/symmetry.ts +1 -1
  46. package/src/tlc/jar-data.ts +1 -3
  47. package/src/tlc/messages.ts +3 -3
  48. package/src/tlc/tlc-operators.ts +3 -1
  49. package/src/tlc/trace-spec.ts +3 -2
  50. package/src/values.ts +4 -4
package/src/resolve.ts CHANGED
@@ -19,6 +19,7 @@ import {
19
19
  type SourceLocation,
20
20
  type SourceRange,
21
21
  type Theorem,
22
+ type UseOrHide,
22
23
  } from "./ast";
23
24
  import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
24
25
  import { resolveSynonym } from "./tlc/jar-data";
@@ -85,8 +86,15 @@ interface Unit {
85
86
  isGenerated: boolean;
86
87
  }
87
88
 
88
- interface SelectionError {
89
+ export interface SelectionArguments {
90
+ operator: string;
91
+ range: SourceRange | undefined;
89
92
  args: Expression[];
93
+ params: number[];
94
+ }
95
+
96
+ interface SelectionError {
97
+ applications: SelectionArguments[];
90
98
  error: SanyError;
91
99
  }
92
100
 
@@ -130,6 +138,9 @@ const CORE_SYMBOLS: Symbols = new Map([
130
138
  export const isBefore = (left: SourceLocation, right: SourceLocation) =>
131
139
  left.line < right.line || (left.line === right.line && left.column < right.column);
132
140
 
141
+ const compareLocations = (left: SourceLocation, right: SourceLocation) =>
142
+ isBefore(left, right) ? -1 : isBefore(right, left) ? 1 : 0;
143
+
133
144
  export const getArity = (definition: Definition) =>
134
145
  definition.kind === "Operator" ? definition.params.length : 0;
135
146
 
@@ -245,7 +256,7 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
245
256
  const isVisible =
246
257
  definition !== undefined &&
247
258
  (definition === unit.self
248
- ? definition.kind === "Function" || definition.isRecursive
259
+ ? definition.isRecursive
249
260
  : definition.isRecursive
250
261
  ? getModuleBinding(name, unit, undefined) !== undefined
251
262
  : isBefore(definition.location, unit.location));
@@ -255,14 +266,17 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
255
266
  const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
256
267
  const definition = getVisibleDefinition(name, unit);
257
268
  if (definition) {
269
+ const declared = definition.isRecursive ? unit.bindings.get(name) : undefined;
258
270
  return {
259
271
  key: unit.getKey(definition),
260
- arity: getArity(definition),
261
- params: getParamArities(definition),
272
+ arity: declared?.arity ?? getArity(definition),
273
+ params: declared?.params ?? getParamArities(definition),
262
274
  kind: "Definition",
263
275
  };
264
276
  }
265
- return getModuleBinding(name, unit, undefined) ? unit.imported.get(name) : undefined;
277
+ const binding = getModuleBinding(name, unit, undefined);
278
+ if (binding?.isPending) return { key: name, arity: binding.arity, kind: binding.kind };
279
+ return binding ? unit.imported.get(name) : undefined;
266
280
  };
267
281
 
268
282
  const isFact = (name: string, unit: Unit) =>
@@ -550,10 +564,17 @@ const checkSelection = (
550
564
  state.isSufficesOpen = false;
551
565
  continue;
552
566
  }
553
- if (selector.kind !== "Operand") return;
554
- const operand = getOperandName(selector.operand);
567
+ if (selector.kind === "Colon") return;
568
+ const operand =
569
+ selector.kind === "Operand"
570
+ ? getOperandName(selector.operand)
571
+ : selector.kind === "At"
572
+ ? "@"
573
+ : "!(...)";
574
+ const getPosition = (count: number) =>
575
+ selector.kind === "Operand" ? getOperandIndex(selector.operand, count) : -1;
555
576
  if (state.isSufficesOpen) {
556
- if (getOperandIndex(selector.operand, 1) !== 0) {
577
+ if (getPosition(1) !== 0) {
557
578
  return report(
558
579
  index,
559
580
  node.kind === "AssumeProve"
@@ -578,6 +599,7 @@ const checkSelection = (
578
599
  return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
579
600
  }
580
601
  const sets = getBinder(expression)?.sets;
602
+ if (sets && selector.kind !== "Operand") return;
581
603
  const operands =
582
604
  expression.kind === "Identifier"
583
605
  ? []
@@ -589,14 +611,14 @@ const checkSelection = (
589
611
  ? undefined
590
612
  : getOperands(expression);
591
613
  if (!operands) return;
592
- const position = getOperandIndex(selector.operand, operands.length);
614
+ const position = getPosition(operands.length);
593
615
  if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
594
616
  state.node = operands[position];
595
617
  state.isGoal = false;
596
618
  continue;
597
619
  }
598
620
  const clauses = [...node.assumptions, node.conclusion];
599
- const position = getOperandIndex(selector.operand, clauses.length);
621
+ const position = getPosition(clauses.length);
600
622
  if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
601
623
  if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
602
624
  return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
@@ -823,6 +845,21 @@ const resolveDefinition = (
823
845
  ? `Function name \`${name}' already defined or declared.`
824
846
  : `Operator ${resolveSynonym(name)} already defined or declared.`,
825
847
  );
848
+ } else if (current && definition.kind === "Function" && current.arity > 0) {
849
+ report(`Function ${resolveSynonym(name)} has operator arguments in its RECURSIVE declaration.`);
850
+ } else if (
851
+ current &&
852
+ definition.kind === "Operator" &&
853
+ // Deviation from SANY: Generator#processOperator compares only the last parameter's arity with the RECURSIVE declaration's.
854
+ (current.arity !== definition.params.length ||
855
+ definition.params.some((param) => param.arity > 0))
856
+ ) {
857
+ report(
858
+ `Definition of ${resolveSynonym(name)} has different arity than its RECURSIVE declaration.`,
859
+ );
860
+ }
861
+ if (current && isDeclared && definition.kind === "Operator") {
862
+ current.params = getParamArities(definition);
826
863
  }
827
864
  if (definition.kind === "Function") {
828
865
  resolveExpression(
@@ -834,8 +871,10 @@ const resolveDefinition = (
834
871
  definition.isSelfReferential = isReferenced(definition.body, name);
835
872
  } else resolveExpression(definition.body, inner, unit);
836
873
  if (current && isDeclared) {
837
- current.isPending = false;
838
- current.range = range;
874
+ if (definition.kind === "Operator" || current.arity === 0) {
875
+ current.isPending = false;
876
+ current.range = range;
877
+ }
839
878
  return false;
840
879
  }
841
880
  return definition.kind === "Function"
@@ -853,10 +892,15 @@ const resolveDefinitions = (
853
892
  scope: LocalName | undefined,
854
893
  unit: Unit,
855
894
  instances: Instance[] = [],
895
+ declarations: Declaration[] = [],
856
896
  ) => {
857
897
  const recursive = new Set<Binding>();
858
898
  const declared = definitions
859
- .filter((definition) => definition.isRecursive)
899
+ .filter(
900
+ (definition) =>
901
+ definition.isRecursive &&
902
+ declarations.every((declaration) => declaration.name !== definition.name),
903
+ )
860
904
  .reduce<LocalName | undefined>((parent, definition) => {
861
905
  if (lookupLocal(parent, definition.name)?.isPending) return parent;
862
906
  const local: LocalName = {
@@ -870,20 +914,47 @@ const resolveDefinitions = (
870
914
  recursive.add(local);
871
915
  return local;
872
916
  }, scope);
873
- const pending = unit.isGenerated ? [] : [...instances];
874
- const resolveInstances = (
917
+ const declaredLocals = new Map<Declaration, LocalName | undefined>();
918
+ const bindDeclaration = (parent: LocalName | undefined, declaration: Declaration) => {
919
+ const binding: Binding = {
920
+ kind: "Definition",
921
+ arity: declaration.arity,
922
+ params: Array(declaration.arity).fill(0),
923
+ range: declaration.location.range,
924
+ };
925
+ const local: LocalName | undefined = isBindable(
926
+ declaration.name,
927
+ lookupLocal(parent, declaration.name) ??
928
+ getModuleBinding(declaration.name, unit, binding.range),
929
+ binding,
930
+ unit,
931
+ )
932
+ ? { ...binding, name: declaration.name, parent, isPending: true }
933
+ : undefined;
934
+ if (local) recursive.add(local);
935
+ declaredLocals.set(declaration, local);
936
+ return local ?? parent;
937
+ };
938
+ const pending = [...(unit.isGenerated ? [] : instances), ...declarations].toSorted(
939
+ (left, right) => compareLocations(left.location, right.location),
940
+ );
941
+ const resolvePending = (
875
942
  current: LocalName | undefined,
876
- isDue: (instance: Instance) => boolean,
943
+ isDue: (item: Instance | Declaration) => boolean,
877
944
  ) => {
878
- const count = pending.findIndex((instance) => !isDue(instance));
945
+ const count = pending.findIndex((item) => !isDue(item));
879
946
  return pending
880
947
  .splice(0, count === -1 ? pending.length : count)
881
- .reduce((parent, instance) => resolveLocalInstance(instance, parent, unit), current);
948
+ .reduce(
949
+ (parent, item) =>
950
+ "kind" in item ? resolveLocalInstance(item, parent, unit) : bindDeclaration(parent, item),
951
+ current,
952
+ );
882
953
  };
883
954
  const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
884
955
  const current = isKey(definition.name)
885
956
  ? previous
886
- : resolveInstances(previous, (instance) => !isBefore(definition.location, instance.location));
957
+ : resolvePending(previous, (item) => !isBefore(definition.location, item.location));
887
958
  const local: LocalName = {
888
959
  name: definition.name,
889
960
  arity: getArity(definition),
@@ -903,7 +974,17 @@ const resolveDefinitions = (
903
974
  ? { ...local, params: getParamArities(definition) }
904
975
  : current;
905
976
  }, declared);
906
- return resolveInstances(resolved, () => true);
977
+ const resolvedScope = resolvePending(resolved, () => true);
978
+ declaredLocals.forEach((local, declaration) => {
979
+ const range = declaration.location.range;
980
+ if (range && !unit.isGenerated && (!local || local.isPending)) {
981
+ unit.errors.push({
982
+ location: formatRange(range),
983
+ message: `Symbol ${resolveSynonym(declaration.name)} declared in RECURSIVE statement but not defined.`,
984
+ });
985
+ }
986
+ });
987
+ return resolvedScope;
907
988
  };
908
989
 
909
990
  const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
@@ -940,6 +1021,100 @@ const isInstanceArityReported = (
940
1021
  return true;
941
1022
  };
942
1023
 
1024
+ const isArgumentCountReported = (
1025
+ callee: Binding | undefined,
1026
+ name: string,
1027
+ count: number,
1028
+ range: SourceRange | undefined,
1029
+ unit: Unit,
1030
+ ) => {
1031
+ const params = callee?.kind === "Definition" ? callee.params : undefined;
1032
+ if (!callee || !params || params.length === callee.arity) return false;
1033
+ if (range) {
1034
+ unit.errors.push({
1035
+ location: formatRange(range),
1036
+ message: `Wrong number of arguments (${count}) given to operator '${resolveSynonym(name)}', \nwhich requires ${params.length} arguments.`,
1037
+ });
1038
+ }
1039
+ return true;
1040
+ };
1041
+
1042
+ const matchArgument = (
1043
+ arg: Expression,
1044
+ expected: number,
1045
+ index: number,
1046
+ operator: string,
1047
+ operatorRange: SourceRange | undefined,
1048
+ scope: LocalName | undefined,
1049
+ unit: Unit,
1050
+ ) => {
1051
+ const report = (range: SourceRange | undefined, message: string) => {
1052
+ if (range) unit.errors.push({ location: formatRange(range), message });
1053
+ };
1054
+ const range = arg.location.range;
1055
+ if (expected === 0) {
1056
+ // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
1057
+ if (arg.kind === "Lambda" && arg.operator !== undefined) {
1058
+ report(range, `The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
1059
+ } else if (arg.kind === "Lambda" && range) {
1060
+ report(range, "LAMBDA expression used where an expression is required.");
1061
+ } else if (arg.kind === "OperatorReference") {
1062
+ const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1063
+ if (argArity !== undefined) {
1064
+ report(
1065
+ range,
1066
+ `The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`,
1067
+ );
1068
+ }
1069
+ } else resolveExpression(arg, scope, unit);
1070
+ return true;
1071
+ }
1072
+ if (arg.kind === "Lambda") {
1073
+ if (selections.has(arg)) resolveExpression(arg, scope, unit);
1074
+ else resolveLambda(arg, scope, unit);
1075
+ const isMatched = arg.params.length === expected;
1076
+ if (!isMatched && selections.has(arg)) {
1077
+ report(range, `Expected arity ${expected} but found operator of arity ${arg.params.length}.`);
1078
+ } else if (!isMatched && range && arg.operator === undefined) {
1079
+ report(
1080
+ operatorRange,
1081
+ `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1082
+ );
1083
+ }
1084
+ return isMatched;
1085
+ }
1086
+ if (arg.kind === "Subexpression") {
1087
+ resolveExpression(arg, scope, unit);
1088
+ return true;
1089
+ }
1090
+ if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
1091
+ const selectorIndex = arg.argumentCounts.findIndex((count) => count > 0);
1092
+ report(
1093
+ arg.selectorRanges?.[selectorIndex],
1094
+ `Selector \`${arg.operator.split("!")[selectorIndex]}' should not have argument(s).`,
1095
+ );
1096
+ return false;
1097
+ }
1098
+ if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
1099
+ report(
1100
+ range,
1101
+ `An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
1102
+ );
1103
+ return false;
1104
+ }
1105
+ const name = arg.kind === "Identifier" ? arg.name : arg.operator;
1106
+ const argParams = findBinding(name, scope, unit)?.params ?? [];
1107
+ const argArity = getSymbolArity(arg, name, scope, unit);
1108
+ if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
1109
+ report(
1110
+ getSpelledNameRange(arg, name),
1111
+ `Expected arity ${expected} but found operator of arity ${argArity}.`,
1112
+ );
1113
+ }
1114
+ // Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
1115
+ return argArity === expected && argParams.every((param) => param === 0);
1116
+ };
1117
+
943
1118
  const resolveApply = (
944
1119
  expression: Expression & { kind: "Apply" },
945
1120
  scope: LocalName | undefined,
@@ -957,89 +1132,38 @@ const resolveApply = (
957
1132
  });
958
1133
  return;
959
1134
  }
960
- const params = callee && (callee.kind === "Definition" ? callee.params : []);
1135
+ const params =
1136
+ callee && (callee.kind === "Definition" ? callee.params : expression.args.map(() => 0));
961
1137
  if (params === undefined) {
962
1138
  expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
963
1139
  return;
964
1140
  }
965
- const report = (range: SourceRange | undefined, message: string) => {
966
- if (range) unit.errors.push({ location: formatRange(range), message });
967
- };
968
- expression.args
969
- .map((arg, index) => {
970
- const expected = params[index] ?? 0;
971
- const range = arg.location.range;
972
- if (expected === 0) {
973
- // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
974
- if (arg.kind === "Lambda" && arg.operator !== undefined) {
975
- report(range, `The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
976
- } else if (arg.kind === "Lambda" && range) {
977
- report(range, "LAMBDA expression used where an expression is required.");
978
- } else if (arg.kind === "OperatorReference") {
979
- const argArity = getSymbolArity(arg, arg.operator, scope, unit);
980
- if (argArity !== undefined) {
981
- report(
982
- range,
983
- `The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`,
984
- );
985
- }
986
- } else resolveExpression(arg, scope, unit);
987
- return true;
988
- }
989
- if (arg.kind === "Lambda") {
990
- resolveExpression(arg, scope, unit);
991
- const isMatched = arg.params.length === expected;
992
- if (!isMatched && selections.has(arg)) {
993
- report(
994
- range,
995
- `Expected arity ${expected} but found operator of arity ${arg.params.length}.`,
996
- );
997
- } else if (!isMatched && range && arg.operator === undefined) {
998
- report(
999
- expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator),
1000
- `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1001
- );
1002
- }
1003
- return isMatched;
1004
- }
1005
- if (arg.kind === "Subexpression") {
1006
- resolveExpression(arg, scope, unit);
1007
- return true;
1008
- }
1009
- if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
1010
- const selectorIndex = arg.argumentCounts.findIndex((count) => count > 0);
1011
- report(
1012
- arg.selectorRanges?.[selectorIndex],
1013
- `Selector \`${arg.operator.split("!")[selectorIndex]}' should not have argument(s).`,
1014
- );
1015
- return false;
1016
- }
1017
- if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
1018
- report(
1019
- range,
1020
- `An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
1021
- );
1022
- return false;
1023
- }
1024
- const name = arg.kind === "Identifier" ? arg.name : arg.operator;
1025
- const argParams = findBinding(name, scope, unit)?.params ?? [];
1026
- const argArity = getSymbolArity(arg, name, scope, unit);
1027
- if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
1028
- report(
1029
- getSpelledNameRange(arg, name),
1030
- `Expected arity ${expected} but found operator of arity ${argArity}.`,
1031
- );
1032
- }
1033
- return argArity === expected && argParams.every((param) => param === 0);
1034
- })
1035
- .forEach((isMatched, index) => {
1036
- if (!isMatched) {
1037
- report(
1038
- expression.location.range,
1039
- `Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
1040
- );
1041
- }
1042
- });
1141
+ const operatorRange =
1142
+ expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator);
1143
+ const matches = expression.args
1144
+ .slice(0, params.length)
1145
+ .map((arg, index) =>
1146
+ matchArgument(arg, params[index], index, operator, operatorRange, scope, unit),
1147
+ );
1148
+ if (
1149
+ isArgumentCountReported(
1150
+ callee,
1151
+ operator,
1152
+ expression.args.length,
1153
+ expression.location.range,
1154
+ unit,
1155
+ )
1156
+ ) {
1157
+ return;
1158
+ }
1159
+ matches.forEach((isMatched, index) => {
1160
+ if (!isMatched && expression.location.range) {
1161
+ unit.errors.push({
1162
+ location: formatRange(expression.location.range),
1163
+ message: `Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
1164
+ });
1165
+ }
1166
+ });
1043
1167
  };
1044
1168
 
1045
1169
  const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
@@ -1050,11 +1174,37 @@ const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Un
1050
1174
  const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
1051
1175
  const failed = selectionErrors.get(expression);
1052
1176
  if (!failed) return false;
1053
- failed.args.forEach((arg) => resolveArgument(arg, scope, unit));
1177
+ failed.applications.forEach((application) =>
1178
+ application.args.forEach((arg, index) =>
1179
+ matchArgument(
1180
+ arg,
1181
+ application.params[index],
1182
+ index,
1183
+ application.operator,
1184
+ application.range,
1185
+ scope,
1186
+ unit,
1187
+ ),
1188
+ ),
1189
+ );
1054
1190
  unit.errors.push(failed.error);
1055
1191
  return true;
1056
1192
  };
1057
1193
 
1194
+ const resolveLambda = (
1195
+ expression: Expression & { kind: "Lambda" },
1196
+ scope: LocalName | undefined,
1197
+ unit: Unit,
1198
+ ) =>
1199
+ resolveExpression(
1200
+ expression.body,
1201
+ bindNames(scope, expression.params, expression.paramRanges, unit),
1202
+ {
1203
+ ...unit,
1204
+ labelParams: [...unit.labelParams, ...expression.params],
1205
+ },
1206
+ );
1207
+
1058
1208
  const resolveExpression = (
1059
1209
  expression: Expression,
1060
1210
  scope: LocalName | undefined,
@@ -1091,7 +1241,15 @@ const resolveExpression = (
1091
1241
  if (isInstanceArityReported(expression, scope, unit)) return;
1092
1242
  const name = expression.name;
1093
1243
  const arity = getSymbolArity(expression, name, scope, unit);
1094
- if (arity !== undefined && arity > 0) {
1244
+ if (arity === 0) {
1245
+ isArgumentCountReported(
1246
+ findBinding(name, scope, unit),
1247
+ name,
1248
+ 0,
1249
+ getSpelledNameRange(expression, name),
1250
+ unit,
1251
+ );
1252
+ } else if (arity !== undefined) {
1095
1253
  unit.errors.push({
1096
1254
  location: formatRange(getSpelledNameRange(expression, name)),
1097
1255
  message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
@@ -1106,11 +1264,13 @@ const resolveExpression = (
1106
1264
  resolveApply(expression, scope, unit);
1107
1265
  return;
1108
1266
  case "Lambda":
1109
- resolveExpression(
1110
- expression.body,
1111
- bindNames(scope, expression.params, expression.paramRanges, unit),
1112
- bindLabelParams(expression.params),
1113
- );
1267
+ if (selections.has(expression)) resolveLambda(expression, scope, unit);
1268
+ else if (expression.location.range) {
1269
+ unit.errors.push({
1270
+ location: formatRange(expression.location.range),
1271
+ message: "LAMBDA expression used where an expression is required.",
1272
+ });
1273
+ }
1114
1274
  return;
1115
1275
  case "And":
1116
1276
  case "Or":
@@ -1133,7 +1293,13 @@ const resolveExpression = (
1133
1293
  case "Let":
1134
1294
  resolveExpression(
1135
1295
  expression.body,
1136
- resolveDefinitions(expression.definitions, scope, unit, expression.instances),
1296
+ resolveDefinitions(
1297
+ expression.definitions,
1298
+ scope,
1299
+ unit,
1300
+ expression.instances,
1301
+ expression.recursive,
1302
+ ),
1137
1303
  unit,
1138
1304
  );
1139
1305
  return;
@@ -1279,6 +1445,13 @@ const resolveExpression = (
1279
1445
 
1280
1446
  const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
1281
1447
  facts.forEach((fact) => {
1448
+ if (fact.kind === "ExceptAt" && fact.location.range) {
1449
+ unit.errors.push({
1450
+ location: formatRange(fact.location.range),
1451
+ message: "Unknown operator: `@'.",
1452
+ });
1453
+ return;
1454
+ }
1282
1455
  if (
1283
1456
  fact.kind === "ModuleFact" ||
1284
1457
  isInstanceArityReported(fact, scope, unit) ||
@@ -1296,6 +1469,87 @@ const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
1296
1469
  } else if (!isFact(name, unit)) resolveExpression(fact, scope, unit);
1297
1470
  });
1298
1471
 
1472
+ const isDefinitionEntry = (def: Fact, scope: LocalName | undefined, unit: Unit) => {
1473
+ const range = def.location.range;
1474
+ const name =
1475
+ def.kind === "Identifier" ? def.name : def.kind === "OperatorReference" ? def.operator : "";
1476
+ if (name === "" || !range) return true;
1477
+ const binding =
1478
+ lookupLocal(scope, name) ?? findSymbol(name, unit) ?? getModuleBinding(name, unit, range);
1479
+ if (binding === undefined && (isFact(name, unit) || isStepName(name))) return true;
1480
+ if (binding === undefined) {
1481
+ unit.errors.push({
1482
+ location: formatRange(range),
1483
+ message: `Unknown operator: \`${resolveSynonym(name)}'.`,
1484
+ });
1485
+ }
1486
+ if (binding && SELECTABLE_KINDS.has(binding.kind) && !isStepName(name)) return true;
1487
+ unit.errors.push({
1488
+ location: formatRange(range),
1489
+ message: "DEF clause entry should describe a defined operator.",
1490
+ });
1491
+ return false;
1492
+ };
1493
+
1494
+ const getHiddenExpressionLocation = (fact: Fact, scope: LocalName | undefined, unit: Unit) => {
1495
+ const range = fact.location.range && formatRange(fact.location.range);
1496
+ if (fact.kind === "ModuleFact" || selections.has(fact)) return undefined;
1497
+ if (selectionErrors.has(fact) || fact.kind === "ExceptAt") return BUILTIN_LOCATION;
1498
+ switch (fact.kind) {
1499
+ case "Number":
1500
+ case "String":
1501
+ case "Decimal":
1502
+ case "Let":
1503
+ case "Label":
1504
+ return undefined;
1505
+ case "Identifier":
1506
+ case "Apply":
1507
+ case "Subexpression": {
1508
+ const first = getSelectors(fact)[0];
1509
+ if (fact.kind === "Subexpression" || first.kind !== "Name") return undefined;
1510
+ const name = fact.kind === "Identifier" ? fact.name : fact.operator;
1511
+ const goal = lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit);
1512
+ if (isFact(name, unit) || goal || (isStepName(name) && lookupLocal(scope, name))) {
1513
+ return undefined;
1514
+ }
1515
+ const binding = findBinding(name, scope, unit);
1516
+ const count = fact.kind === "Apply" ? fact.args.length : 0;
1517
+ return binding?.arity === count ? range : BUILTIN_LOCATION;
1518
+ }
1519
+ default:
1520
+ return range;
1521
+ }
1522
+ };
1523
+
1524
+ const checkUseOrHide = (
1525
+ statement: UseOrHide | Extract<Proof, { kind: "By" }>,
1526
+ scope: LocalName | undefined,
1527
+ unit: Unit,
1528
+ ) => {
1529
+ const hidden =
1530
+ statement.kind === "Hide"
1531
+ ? statement.facts.map((fact) => getHiddenExpressionLocation(fact, scope, unit))
1532
+ : [];
1533
+ checkFacts(statement.facts, scope, unit);
1534
+ const defCount = statement.defs.filter((def) => isDefinitionEntry(def, scope, unit)).length;
1535
+ hidden.forEach((location) => {
1536
+ if (location) {
1537
+ unit.errors.push({
1538
+ location,
1539
+ message:
1540
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1541
+ });
1542
+ }
1543
+ });
1544
+ const range = statement.location.range;
1545
+ if (statement.facts.length + defCount === 0 && range) {
1546
+ unit.errors.push({
1547
+ location: formatRange(range),
1548
+ message: statement.kind === "By" ? "Empty BY" : "Empty USE or HIDE statement.",
1549
+ });
1550
+ }
1551
+ };
1552
+
1299
1553
  const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
1300
1554
  if (symbol.set) resolveExpression(symbol.set, scope, unit);
1301
1555
  return bindName(
@@ -1343,7 +1597,7 @@ const resolveStatement = (
1343
1597
 
1344
1598
  const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
1345
1599
  const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
1346
- if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
1600
+ if (proof?.kind === "By") checkUseOrHide(proof, scope, unit);
1347
1601
  if (proof?.kind !== "Steps") return;
1348
1602
  proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
1349
1603
  };
@@ -1392,7 +1646,7 @@ const resolveStep = (
1392
1646
  }
1393
1647
  case "Use":
1394
1648
  case "Hide":
1395
- checkFacts(body.facts, scope, unit);
1649
+ checkUseOrHide(body, scope, unit);
1396
1650
  return bindNonFactStep(scope);
1397
1651
  case "Instance":
1398
1652
  return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
@@ -1527,7 +1781,7 @@ const checkSubstitutions = (
1527
1781
  return;
1528
1782
  }
1529
1783
  if (expression.kind === "Lambda") {
1530
- resolveExpression(expression, scope, unit);
1784
+ resolveLambda(expression, scope, unit);
1531
1785
  if (expression.params.length !== declaration.arity && range) {
1532
1786
  reportArity(declaration, formatRange(range));
1533
1787
  }
@@ -1633,7 +1887,7 @@ const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit:
1633
1887
  const instanced = unit.getInstance(instance);
1634
1888
  checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
1635
1889
  return [...instanced.symbols].reduce(
1636
- (parent, [name, symbol]) => bindName(parent, name, symbol, unit),
1890
+ (parent, entry) => bindName(parent, entry[0], entry[1], unit),
1637
1891
  scope,
1638
1892
  );
1639
1893
  };
@@ -1663,9 +1917,9 @@ export const resolveSubstitutions = (
1663
1917
  facts: new Set(),
1664
1918
  goals: new Map(),
1665
1919
  bindings: new Map(
1666
- [...imported].map(([name, symbol]) => [
1667
- name,
1668
- { ...symbol, position: positions.get(name) ?? MODULE_START },
1920
+ [...imported].map((entry) => [
1921
+ entry[0],
1922
+ { ...entry[1], position: positions.get(entry[0]) ?? MODULE_START },
1669
1923
  ]),
1670
1924
  ),
1671
1925
  errors: report.errors,
@@ -1696,13 +1950,7 @@ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError
1696
1950
  }
1697
1951
  };
1698
1952
  units
1699
- .toSorted((left, right) =>
1700
- isBefore(left.location, right.location)
1701
- ? -1
1702
- : isBefore(right.location, left.location)
1703
- ? 1
1704
- : 0,
1705
- )
1953
+ .toSorted((left, right) => compareLocations(left.location, right.location))
1706
1954
  .forEach((unit) => {
1707
1955
  const reported: SanyError[] = [];
1708
1956
  const warned: SanyError[] = [];
@@ -1730,9 +1978,9 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1730
1978
  ),
1731
1979
  );
1732
1980
  const bindings = new Map<string, ModuleBinding>(
1733
- [...CORE_SYMBOLS, ...unit.bound].map(([name, symbol]) => [
1734
- name,
1735
- { ...symbol, position: MODULE_START },
1981
+ [...CORE_SYMBOLS, ...unit.bound].map((entry) => [
1982
+ entry[0],
1983
+ { ...entry[1], position: MODULE_START },
1736
1984
  ]),
1737
1985
  );
1738
1986
  const recursive = new Set<Binding>();
@@ -1824,6 +2072,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1824
2072
  const binding: ModuleBinding = {
1825
2073
  kind: "Definition",
1826
2074
  arity: declaration.arity,
2075
+ params: Array(declaration.arity).fill(0),
1827
2076
  range: declaration.location.range,
1828
2077
  position: declaration.location,
1829
2078
  isPending: true,
@@ -1889,7 +2138,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1889
2138
  ),
1890
2139
  ...module.uses.map((use) =>
1891
2140
  createCheck(use.location, undefined, false, (inner) =>
1892
- checkFacts(use.facts, undefined, inner),
2141
+ checkUseOrHide(use, undefined, inner),
1893
2142
  ),
1894
2143
  ),
1895
2144
  {