tla-ts 0.0.3-dev.33ae522 → 0.0.3-dev.3504663

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/resolve.ts CHANGED
@@ -6,12 +6,15 @@ import {
6
6
  type BoundPattern,
7
7
  type Definition,
8
8
  type Expression,
9
+ type Fact,
9
10
  type Instance,
11
+ type LabelExpression,
10
12
  type Module,
11
13
  type NewSymbol,
12
14
  type OperatorParam,
13
15
  type Proof,
14
16
  type ProofStep,
17
+ type Selector,
15
18
  type SourceLocation,
16
19
  type SourceRange,
17
20
  type Theorem,
@@ -32,12 +35,24 @@ export interface ModuleSymbol {
32
35
 
33
36
  export type Symbols = Map<string, ModuleSymbol>;
34
37
 
38
+ // A named theorem or proof step whose parts a name such as foo!lab or <1>2!1 selects. isInProof
39
+ // holds while SANY analyzes the proof of the theorem or step.
40
+ interface Goal {
41
+ statement: Expression | AssumeProve;
42
+ isSuffices: boolean;
43
+ isInProof: boolean;
44
+ }
45
+
35
46
  interface LocalName {
36
47
  name: string;
37
48
  arity: number;
38
49
  parent: LocalName | undefined;
50
+ goal?: Goal;
39
51
  }
40
52
 
53
+ // errors collects the errors SANY reports while it goes on analyzing the unit. A label may appear
54
+ // where hasLabelScope holds, in a definition, a named theorem, or a numbered step,
55
+ // except where isLabelForbidden holds, in the scope of a NEW in a nested ASSUME.
41
56
  interface Unit {
42
57
  imported: Symbols;
43
58
  definitions: Map<string, Definition>;
@@ -45,8 +60,20 @@ interface Unit {
45
60
  location: SourceLocation;
46
61
  self: Definition | undefined;
47
62
  facts: Set<string>;
63
+ goals: Map<string, Goal>;
64
+ errors: SanyError[];
65
+ hasLabelScope: boolean;
66
+ isLabelForbidden: boolean;
48
67
  }
49
68
 
69
+ interface SelectionState {
70
+ node: Expression | NewSymbol | AssumeProve;
71
+ isGoal: boolean;
72
+ isSufficesOpen: boolean;
73
+ }
74
+
75
+ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" };
76
+
50
77
  export const LOCAL_RESOLUTION = { kind: "Local" } as const;
51
78
 
52
79
  const renamedArities = new WeakMap<Expression, number>();
@@ -134,6 +161,185 @@ const isOperatorSymbol = (name: string) =>
134
161
  const isStepName = (name: string) =>
135
162
  name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
136
163
 
164
+ export const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
165
+ if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
166
+ if (operand === "<<") return count > 0 ? 0 : -1;
167
+ return count === 2 ? 1 : -1;
168
+ };
169
+
170
+ const getSelectors = (reference: Reference): Selector[] =>
171
+ reference.kind === "Subexpression"
172
+ ? reference.selectors
173
+ : (reference.kind === "Identifier" ? reference.name : reference.operator)
174
+ .split("!")
175
+ .map((name) => ({ kind: "Name", name, args: [] }));
176
+
177
+ const isReference = (expression: Expression): expression is Reference =>
178
+ expression.kind === "Identifier" ||
179
+ expression.kind === "Apply" ||
180
+ expression.kind === "Subexpression";
181
+
182
+ const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
183
+ assumeProve.assumptions.slice(0, clause).some((assumption) => assumption.kind === "NewSymbol");
184
+
185
+ const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
186
+ if (expression.kind === "Label") return expression.name === name ? expression : undefined;
187
+ if (expression.kind === "Lambda") return undefined;
188
+ if (expression.kind === "Let") return findLabel(expression.body, name);
189
+ return getChildren(expression).reduce<LabelExpression | undefined>(
190
+ (found, child) => found ?? findLabel(child, name),
191
+ undefined,
192
+ );
193
+ };
194
+
195
+ // SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
196
+ const findClauseLabel = (
197
+ clause: Expression | NewSymbol | AssumeProve,
198
+ name: string,
199
+ isForbidden: boolean,
200
+ ): LabelExpression | undefined => {
201
+ if (clause.kind === "AssumeProve") {
202
+ return [...clause.assumptions, clause.conclusion].reduce<LabelExpression | undefined>(
203
+ (found, inner, index) =>
204
+ found ?? findClauseLabel(inner, name, isForbidden || isInScopeOfDeclaration(clause, index)),
205
+ undefined,
206
+ );
207
+ }
208
+ if (isForbidden) return undefined;
209
+ if (clause.kind === "NewSymbol") return clause.set && findLabel(clause.set, name);
210
+ return findLabel(clause, name);
211
+ };
212
+
213
+ const getOperandName = (operand: number | "<<" | ">>") =>
214
+ typeof operand === "number" ? `argument selector ${operand}` : operand;
215
+
216
+ const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
217
+
218
+ // Generator.selectorToNode for a name that selects part of a theorem or step. A part in the scope of
219
+ // a NEW in its ASSUME may be selected only within the proof of the theorem, or only after the proof
220
+ // of a SUFFICES step. A fact may select an ASSUME/PROVE or a NEW clause.
221
+ const checkSelection = (
222
+ reference: Reference,
223
+ selectors: Selector[],
224
+ goal: Goal,
225
+ unit: Unit,
226
+ isFact: boolean,
227
+ ) => {
228
+ const report = (index: number | undefined, message: string) => {
229
+ const range =
230
+ (index === undefined ? undefined : reference.selectorRanges?.[index]) ??
231
+ reference.location.range;
232
+ if (range) unit.errors.push({ location: formatRange(range), message });
233
+ };
234
+ const isOutOfScope = goal.isSuffices === goal.isInProof;
235
+ const statement = goal.statement;
236
+ const second = selectors[1];
237
+ if (second.kind === "Name") {
238
+ const clauses =
239
+ statement.kind === "AssumeProve"
240
+ ? [...statement.assumptions, statement.conclusion]
241
+ : [statement];
242
+ const found = clauses
243
+ .map((clause, index) => ({ index, label: findClauseLabel(clause, second.name, false) }))
244
+ .find((item) => item.label !== undefined);
245
+ if (!found) return report(1, `Cannot find label \`${second.name}'.`);
246
+ if (
247
+ statement.kind === "AssumeProve" &&
248
+ isInScopeOfDeclaration(statement, found.index) &&
249
+ isOutOfScope
250
+ ) {
251
+ return report(
252
+ 1,
253
+ `Accessing subexpression labeled \`${second.name}' of ASSUME/PROVE clause ${SCOPE_MESSAGE}`,
254
+ );
255
+ }
256
+ selectors.slice(2).reduce((label, selector, offset) => {
257
+ if (!label || selector.kind !== "Name") return undefined;
258
+ const inner = findLabel(label.body, selector.name);
259
+ if (!inner) report(offset + 2, `Cannot find label \`${selector.name}'.`);
260
+ return inner;
261
+ }, found.label);
262
+ return;
263
+ }
264
+ const state: SelectionState = {
265
+ node: statement,
266
+ isGoal: true,
267
+ isSufficesOpen: goal.isSuffices,
268
+ };
269
+ for (const [offset, selector] of selectors.slice(1).entries()) {
270
+ const index = offset + 1;
271
+ const node = state.node;
272
+ if (selector.kind !== "Operand") return;
273
+ if (state.isSufficesOpen) {
274
+ if (getOperandIndex(selector.operand, 1) !== 0) {
275
+ return report(
276
+ index,
277
+ node.kind === "AssumeProve"
278
+ ? "Accessing non-existent subexpression of a SUFFICES"
279
+ : `Nonexistent operand specified by \`${getOperandName(selector.operand)}'.`,
280
+ );
281
+ }
282
+ state.isSufficesOpen = false;
283
+ continue;
284
+ }
285
+ if (node.kind !== "AssumeProve") return;
286
+ const clauses = [...node.assumptions, node.conclusion];
287
+ const position = getOperandIndex(selector.operand, clauses.length);
288
+ if (position === -1) {
289
+ return report(
290
+ index,
291
+ `Nonexistent operand specified by \`${getOperandName(selector.operand)}'.`,
292
+ );
293
+ }
294
+ if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
295
+ return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
296
+ }
297
+ const clause = clauses[position];
298
+ if (clause.kind === "NewSymbol" && index !== selectors.length - 1) {
299
+ return report(index, "Selected a subexpression of a NEW clause of an ASSUME.");
300
+ }
301
+ state.node = clause;
302
+ state.isGoal = false;
303
+ }
304
+ if (isFact) return;
305
+ if (state.node.kind === "AssumeProve") {
306
+ report(undefined, "Selected ASSUME/PROVE instead of expression.");
307
+ }
308
+ if (state.node.kind === "NewSymbol") {
309
+ report(undefined, "Selected a NEW declaration as an expression or operator.");
310
+ }
311
+ };
312
+
313
+ // Generator.generateLabel reports a label where it may not appear, and skips the label's body.
314
+ const isLabelReported = (label: LabelExpression, unit: Unit) => {
315
+ const message = !unit.hasLabelScope
316
+ ? "Label not in definition or proof step."
317
+ : unit.isLabelForbidden
318
+ ? "Label not allowed within scope of declaration in nested ASSUME/PROVE."
319
+ : undefined;
320
+ if (message === undefined || !label.location.range) return false;
321
+ unit.errors.push({ location: formatRange(label.location.range), message });
322
+ return true;
323
+ };
324
+
325
+ // Checks a name that selects part of a theorem or step, and tells whether it is one.
326
+ const checkReference = (
327
+ expression: Expression,
328
+ scope: LocalName | undefined,
329
+ unit: Unit,
330
+ isFact: boolean,
331
+ ) => {
332
+ if (!isReference(expression)) return false;
333
+ const selectors = getSelectors(expression);
334
+ const first = selectors[0];
335
+ const goal =
336
+ selectors.length > 1 && first.kind === "Name"
337
+ ? (lookupLocal(scope, first.name)?.goal ?? unit.goals.get(first.name))
338
+ : undefined;
339
+ if (goal) checkSelection(expression, selectors, goal, unit, isFact);
340
+ return goal !== undefined;
341
+ };
342
+
137
343
  const getSymbolArity = (
138
344
  expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
139
345
  name: string,
@@ -144,7 +350,9 @@ const getSymbolArity = (
144
350
  const renamed = renamedArities.get(expression);
145
351
  if (renamed !== undefined) return renamed;
146
352
  if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
147
- if (isStepName(name)) return 0;
353
+ if (checkReference(expression, scope, unit, false) || isStepName(name)) {
354
+ return expression.kind === "Apply" ? expression.args.length : 0;
355
+ }
148
356
  const local = lookupLocal(scope, name);
149
357
  if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
150
358
  if (local) return local.arity;
@@ -205,7 +413,8 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
205
413
  getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
206
414
  };
207
415
 
208
- const resolveDefinition = (definition: Definition, scope: LocalName | undefined, unit: Unit) => {
416
+ const resolveDefinition = (definition: Definition, scope: LocalName | undefined, outer: Unit) => {
417
+ const unit = outer.hasLabelScope ? outer : { ...outer, hasLabelScope: true };
209
418
  const self = bindNames(scope, definition.kind === "Function" ? [definition.name] : []);
210
419
  if (definition.kind === "Function") {
211
420
  resolveExpression(definition.body, resolveBounds(definition.bounds, self, unit), unit);
@@ -425,34 +634,67 @@ const resolveExpression = (
425
634
  resolveAll([expression.subscript, expression.action]);
426
635
  return;
427
636
  case "Label":
428
- resolveExpression(expression.body, scope, unit);
637
+ if (!isLabelReported(expression, unit)) resolveExpression(expression.body, scope, unit);
429
638
  return;
430
- case "Subexpression":
639
+ case "Subexpression": {
431
640
  expression.selectors
432
641
  .flatMap((selector) => ("args" in selector ? selector.args : []))
433
642
  .forEach((arg) => resolveArgument(arg, scope, unit));
643
+ checkReference(expression, scope, unit, false);
644
+ }
434
645
  }
435
646
  };
436
647
 
648
+ // SANY analyzes the facts of a proof apart from their names, which may name theorems and modules.
649
+ const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) => {
650
+ const check = (expression: Expression, isFact: boolean, inner: Unit): void => {
651
+ if (expression.kind === "Label" && isLabelReported(expression, inner)) return;
652
+ checkReference(expression, scope, inner, isFact);
653
+ getChildren(expression).forEach((child) =>
654
+ check(
655
+ child,
656
+ false,
657
+ expression.kind === "Let" && child !== expression.body
658
+ ? { ...inner, hasLabelScope: true }
659
+ : inner,
660
+ ),
661
+ );
662
+ };
663
+ facts.forEach((fact) => {
664
+ if (fact.kind !== "ModuleFact") check(fact, true, { ...unit, hasLabelScope: false });
665
+ });
666
+ };
667
+
437
668
  const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
438
669
  if (symbol.set) resolveExpression(symbol.set, scope, unit);
439
670
  return { name: symbol.name, arity: symbol.arity, parent: scope };
440
671
  };
441
672
 
442
- // The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows.
673
+ // The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows. SANY
674
+ // forbids a label in the scope of a NEW in a nested ASSUME.
443
675
  const resolveAssumeProve = (
444
676
  assumeProve: AssumeProve,
445
677
  scope: LocalName | undefined,
446
678
  unit: Unit,
679
+ isNested = false,
447
680
  ): LocalName | undefined => {
448
- const assumed = assumeProve.assumptions.reduce((current, assumption) => {
449
- if (assumption.kind === "NewSymbol") return resolveNewSymbol(assumption, current, unit);
450
- if (assumption.kind === "AssumeProve") resolveAssumeProve(assumption, current, unit);
451
- else resolveExpression(assumption, current, unit);
452
- return current;
453
- }, scope);
454
- resolveExpression(assumeProve.conclusion, assumed, unit);
455
- return assumed;
681
+ const assumed = assumeProve.assumptions.reduce(
682
+ (current, assumption) => {
683
+ if (assumption.kind === "NewSymbol") {
684
+ return {
685
+ scope: resolveNewSymbol(assumption, current.scope, current.unit),
686
+ unit: isNested ? { ...current.unit, isLabelForbidden: true } : current.unit,
687
+ };
688
+ }
689
+ if (assumption.kind === "AssumeProve") {
690
+ resolveAssumeProve(assumption, current.scope, current.unit, true);
691
+ } else resolveExpression(assumption, current.scope, current.unit);
692
+ return current;
693
+ },
694
+ { scope, unit },
695
+ );
696
+ resolveExpression(assumeProve.conclusion, assumed.scope, assumed.unit);
697
+ return assumed.scope;
456
698
  };
457
699
 
458
700
  const resolveStatement = (
@@ -467,7 +709,9 @@ const resolveStatement = (
467
709
 
468
710
  // SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
469
711
  // theorems, assumptions, and modules.
470
- const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, unit: Unit) => {
712
+ const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
713
+ const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
714
+ if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
471
715
  if (proof?.kind !== "Steps") return;
472
716
  proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
473
717
  };
@@ -478,12 +722,24 @@ const resolveStep = (
478
722
  unit: Unit,
479
723
  ): LocalName | undefined => {
480
724
  const body = step.body;
725
+ const stepUnit = step.name === undefined ? unit : { ...unit, hasLabelScope: true };
481
726
  switch (body.kind) {
482
727
  case "Assert": {
483
- const assumed = resolveStatement(body.statement, scope, unit);
484
- resolveProof(step.proof, assumed, unit);
485
- return body.isSuffices ? assumed : scope;
728
+ const assumed = resolveStatement(body.statement, scope, stepUnit);
729
+ const name = step.name;
730
+ if (name === undefined) {
731
+ resolveProof(step.proof, assumed, unit);
732
+ return body.isSuffices ? assumed : scope;
733
+ }
734
+ const goal = { statement: body.statement, isSuffices: body.isSuffices, isInProof: true };
735
+ resolveProof(step.proof, { name, arity: 0, parent: assumed, goal }, unit);
736
+ goal.isInProof = false;
737
+ return { name, arity: 0, parent: body.isSuffices ? assumed : scope, goal };
486
738
  }
739
+ case "Use":
740
+ case "Hide":
741
+ checkFacts(body.facts, scope, unit);
742
+ return scope;
487
743
  case "Define":
488
744
  return resolveDefinitions(
489
745
  body.definitions.flatMap((definition) =>
@@ -494,17 +750,17 @@ const resolveStep = (
494
750
  );
495
751
  case "Have":
496
752
  case "Case":
497
- resolveExpression(body.expression, scope, unit);
753
+ resolveExpression(body.expression, scope, stepUnit);
498
754
  resolveProof(step.proof, scope, unit);
499
755
  return scope;
500
756
  case "Witness":
501
- body.expressions.forEach((expression) => resolveExpression(expression, scope, unit));
757
+ body.expressions.forEach((expression) => resolveExpression(expression, scope, stepUnit));
502
758
  return scope;
503
759
  case "Take":
504
- return bindNames(resolveBounds(body.bounds, scope, unit), body.names);
760
+ return bindNames(resolveBounds(body.bounds, scope, stepUnit), body.names);
505
761
  case "Pick": {
506
- const picked = bindNames(resolveBounds(body.bounds, scope, unit), body.names);
507
- resolveExpression(body.body, picked, unit);
762
+ const picked = bindNames(resolveBounds(body.bounds, scope, stepUnit), body.names);
763
+ resolveExpression(body.body, picked, stepUnit);
508
764
  resolveProof(step.proof, scope, unit);
509
765
  return picked;
510
766
  }
@@ -517,7 +773,10 @@ const resolveStep = (
517
773
  const resolveTheorem = (theorem: Theorem, unit: Unit) => {
518
774
  const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
519
775
  const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
776
+ const goal = unit.goals.get(theorem.name ?? "");
777
+ if (goal) goal.isInProof = true;
520
778
  resolveProof(theorem.proof, scope, unit);
779
+ if (goal) goal.isInProof = false;
521
780
  };
522
781
 
523
782
  interface ModuleUnit {
@@ -777,6 +1036,10 @@ export const resolveSubstitutions = (
777
1036
  location: instance.location,
778
1037
  self: undefined,
779
1038
  facts: new Set(),
1039
+ goals: new Map(),
1040
+ errors: [],
1041
+ hasLabelScope: true,
1042
+ isLabelForbidden: false,
780
1043
  };
781
1044
  const explicit = new Map(instance.substitutions.map((item) => [item.name, item.expression]));
782
1045
  const position = `in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`;
@@ -812,25 +1075,31 @@ export const resolveSubstitutions = (
812
1075
  );
813
1076
  };
814
1077
 
815
- // SANY reports the errors of every unit it checks, in the order of the units in the module.
816
- export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
1078
+ // SANY reports the errors of every unit it checks, in the order of the units in the module. A unit
1079
+ // reports to its list the errors after which SANY goes on, and throws the one that stops it.
1080
+ export const collectSemanticErrors = (
1081
+ units: { location: SourceLocation; check: (reported: SanyError[]) => void }[],
1082
+ ) => {
817
1083
  const errors: SanyError[] = [];
818
1084
  units
819
1085
  .toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
820
1086
  .forEach((unit) => {
1087
+ const reported: SanyError[] = [];
821
1088
  try {
822
- unit.check();
1089
+ unit.check(reported);
823
1090
  } catch (error) {
824
1091
  if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
825
- error.sany.errors
826
- .filter((item) =>
827
- errors.every(
828
- (existing) =>
829
- existing.location !== item.location || existing.message !== item.message,
830
- ),
831
- )
832
- .forEach((item) => errors.push(item));
1092
+ reported.push(...error.sany.errors);
833
1093
  }
1094
+ reported.forEach((item) => {
1095
+ if (
1096
+ errors.every(
1097
+ (existing) => existing.location !== item.location || existing.message !== item.message,
1098
+ )
1099
+ ) {
1100
+ errors.push(item);
1101
+ }
1102
+ });
834
1103
  });
835
1104
  if (errors.length > 0) throw createSanyError(errors);
836
1105
  };
@@ -840,32 +1109,63 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
840
1109
  const facts = new Set(
841
1110
  [...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
842
1111
  );
843
- const createUnit = (location: SourceLocation, self: Definition | undefined) => ({
1112
+ const goals = new Map(
1113
+ module.theorems.flatMap((theorem): [string, Goal][] =>
1114
+ theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
1115
+ ? [[theorem.name, { statement: theorem.statement, isSuffices: false, isInProof: false }]]
1116
+ : [],
1117
+ ),
1118
+ );
1119
+ const createUnit = (
1120
+ location: SourceLocation,
1121
+ self: Definition | undefined,
1122
+ errors: SanyError[],
1123
+ hasLabelScope: boolean,
1124
+ ): Unit => ({
844
1125
  imported: symbols,
845
1126
  definitions: unit.definitions,
846
1127
  getKey: unit.getKey,
847
1128
  location,
848
1129
  self,
849
1130
  facts,
1131
+ goals,
1132
+ errors,
1133
+ hasLabelScope,
1134
+ isLabelForbidden: false,
850
1135
  });
851
1136
  collectSemanticErrors([
852
1137
  ...module.definitions.map((definition) => ({
853
1138
  location: definition.location,
854
- check: () =>
855
- resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
1139
+ check: (errors: SanyError[]) =>
1140
+ resolveDefinition(
1141
+ definition,
1142
+ undefined,
1143
+ createUnit(definition.location, definition, errors, true),
1144
+ ),
856
1145
  })),
1146
+ // An assumption holds the subexpressions its references select, labels and all, so its own
1147
+ // labels go unchecked.
857
1148
  ...module.assumptions.map((assumption) => ({
858
1149
  location: assumption.location,
859
- check: () =>
1150
+ check: (errors: SanyError[]) =>
860
1151
  resolveExpression(
861
1152
  assumption.expression,
862
1153
  undefined,
863
- createUnit(assumption.location, undefined),
1154
+ createUnit(assumption.location, undefined, errors, true),
864
1155
  ),
865
1156
  })),
866
1157
  ...module.theorems.map((theorem) => ({
867
1158
  location: theorem.location,
868
- check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
1159
+ check: (errors: SanyError[]) =>
1160
+ resolveTheorem(
1161
+ theorem,
1162
+ createUnit(theorem.location, undefined, errors, theorem.name !== undefined),
1163
+ ),
1164
+ })),
1165
+ ...module.uses.map((use) => ({
1166
+ location: use.location,
1167
+ check: (errors: SanyError[]) =>
1168
+ checkFacts(use.facts, undefined, createUnit(use.location, undefined, errors, false)),
869
1169
  })),
870
1170
  ]);
871
1171
  };
package/src/run-cli.ts CHANGED
@@ -28,7 +28,7 @@ import type { LineRange } from "./obligation-generate";
28
28
  import { getAtPrefix, getLocusText } from "./obligation-expression";
29
29
  import { formatSummary, getObligations, WHOLE_MODULE, type ModuleObligations } from "./obligations";
30
30
  import { check } from "./parallel";
31
- import { parseModule } from "./parser";
31
+ import { parseModuleFile } from "./sany-parse-error";
32
32
  import type { StateDump } from "./state-graph";
33
33
  import {
34
34
  formatProbability,
@@ -247,7 +247,7 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
247
247
  }
248
248
  const source = readFileSync(specPath, "utf8");
249
249
  const fileName = basename(specPath, ".tla");
250
- const moduleName = parseModule(source).name;
250
+ const moduleName = parseModuleFile(source, fileName).name;
251
251
  if (moduleName !== fileName) {
252
252
  throw createSanyError(
253
253
  [
@@ -333,6 +333,7 @@ const printError = (error: TlaError, path?: string) => {
333
333
  }
334
334
  const report = error instanceof TlaSyntaxError ? error.sany : undefined;
335
335
  if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
336
+ if (report.parseError !== undefined) console.log(report.parseError);
336
337
  console.log(
337
338
  report.isFatal
338
339
  ? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`